Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Define variables to store the data selected by the observer, usable throughout the whole form
- Private currentFormName As String
- Private activityButtonColor As String
- Private partyMembersButtonColor As String
- Private currentIntervalHour As String
- Private currentIntervalMinute As String
- Private currentIntervalRow As String
- Private selectedWeather As String
- Private selectedHeight As String
- Private selectedVisibility As String
- Private selectedActivity As String
- Private selectedVocalization As String
- Private Sub UserForm_Activate()
- Dim wsVars As Worksheet
- 'Define wsVars as the Variables worksheet
- Set wsVars = ThisWorkbook.Sheets("Variables")
- ' Store the name of the form in the worksheet
- currentFormName = Me.Name
- wsVars.Range("currentFormName").Value = currentFormName
- ' Retrieve all the variables needed in this sub at once, and store it in an array, to avoid accessing the Variables sheets multiple times (which slows down excel)
- Dim currentVariables As Variant
- currentVariables = wsVars.Range("E2:S2").Value
- ' Assign values to local variables using the variables stored in the newly created array
- currentIntervalHour = currentVariables(1, 1)
- currentIntervalMinute = currentVariables(1, 2)
- selectedWeather = currentVariables(1, 4)
- selectedHeight = currentVariables(1, 5)
- selectedVisibility = currentVariables(1, 6)
- selectedVocalization = currentVariables(1, 8)
- activityButtonColor = currentVariables(1, 14)
- partyMembersButtonColor = currentVariables(1, 15)
- ' If no color is set, reset to default
- If activityButtonColor = vbNullString Then
- activityButtonColor = &H8000000D
- wsVars.Range("activityButtonColor").Value = activityButtonColor
- End If
- Me.ActivityButton.BackColor = activityButtonColor
- ' Update the label
- LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
- ' Set ComboBoxes if values exist
- If selectedVocalization <> vbNullString Then ComboBoxVocalization.Value = selectedVocalization
- If selectedWeather <> vbNullString Then ComboBoxWeather.Value = selectedWeather
- If selectedHeight <> vbNullString Then ComboBoxHeight.Value = selectedHeight
- ' Set OptionButtons for Visibility
- Select Case selectedVisibility
- Case "0": OptionButton0.Value = True
- Case "1": OptionButton1.Value = True
- Case "2": OptionButton2.Value = True
- Case "Obs.break": OptionButtonObsBreak.Value = True
- Case Else
- OptionButton0.Value = False
- OptionButton1.Value = False
- OptionButton2.Value = False
- OptionButtonObsBreak.Value = False
- End Select
- Set wsVars = Nothing
- End Sub
- Private Sub ComboBoxWeather_Change()
- Dim wsVars As Worksheet
- 'Define wsVars as the Variables worksheet
- Set wsVars = ThisWorkbook.Sheets("Variables")
- 'assign the selected value in ComboBoxWeather to the selectedWeather variable
- selectedWeather = ComboBoxWeather.Value
- wsVars.Range("selectedWeather").Value = selectedWeather
- Set wsVars = Nothing
- End Sub
- Private Sub ComboBoxHeight_Change()
- Dim wsVars As Worksheet
- 'Define wsVars as the Variables worksheet
- Set wsVars = ThisWorkbook.Sheets("Variables")
- 'assign the selected value in ComboBoxHeight to the selectedHeight variable
- selectedHeight = ComboBoxHeight.Value
- wsVars.Range("selectedHeight").Value = selectedHeight
- Set wsVars = Nothing
- End Sub
- Private Sub ComboBoxVocalization_Change()
- Dim wsVars As Worksheet
- 'Define wsVars as the Variables worksheet
- Set wsVars = ThisWorkbook.Sheets("Variables")
- 'assign the selected value in ComboBoxVocalization to the selectedVocalization variable
- selectedVocalization = ComboBoxVocalization.Value
- wsVars.Range("selectedVocalization").Value = selectedVocalization
- Set wsVars = Nothing
- End Sub
- 'The 4 subs below make sure that the local visibility variable is assigned the value of the corresponding OptionButton;
- 'it also attributes a similar index value that we will use in the next sub
- Private Sub OptionButton0_Click()
- HandleOptionButtonClick 0, "0"
- End Sub
- Private Sub OptionButton1_Click()
- HandleOptionButtonClick 1, "1"
- End Sub
- Private Sub OptionButton2_Click()
- HandleOptionButtonClick 2, "2"
- End Sub
- Private Sub OptionButtonObsBreak_Click()
- HandleOptionButtonClick "ObsBreak", "Obs.break"
- End Sub
- ' This sub handles color updates, so the selected OptionButton gets colored in green, and gets back to normal when unselected
- 'It also updates the local visibility variable so it corresponds to the selected button
- Private Sub HandleOptionButtonClick(ByVal Index As Variant, ByVal visibility As String)
- 'Update the value of the local visibility variable
- HandleVisibilityChange visibility
- ' Manually reset colors for all OptionButtons
- OptionButton0.BackColor = &HC0C0C0 ' Default color
- OptionButton1.BackColor = &HC0C0C0
- OptionButton2.BackColor = &HC0C0C0
- OptionButtonObsBreak.BackColor = &HFFFFC0
- ' Change color of the selected OptionButton
- If IsNumeric(Index) Then
- Me.Controls("OptionButton" & Index).BackColor = RGB(0, 200, 100) ' Green
- Else
- 'as OptionButtonObsBreak does not have a numerical index, we need to handle it separately
- Me.Controls("OptionButtonObsBreak").BackColor = RGB(0, 200, 100) ' Green
- End If
- End Sub
- Private Sub HandleVisibilityChange(ByVal visibility As String)
- Dim wsVars As Worksheet
- 'Define wsVars as the Variables worksheet
- Set wsVars = ThisWorkbook.Sheets("Variables")
- ' Update the selectedVisibility variable, using the local visibility variable
- selectedVisibility = visibility
- wsVars.Range("selectedVisibility").Value = selectedVisibility
- Set wsVars = Nothing
- End Sub
- Private Sub CommandButtonSocialNotes_Click()
- socialNotesForm.Show
- End Sub
- Private Sub CommandButtonSaveAndNext_Click()
- Dim colIndexHour As Long, colIndexMinute As Long
- Dim wsActivity As Worksheet
- Dim wsVars As Worksheet
- Dim dataArray As Variant
- Dim saveInterval As Integer
- ' Define worksheets
- Set wsActivity = ThisWorkbook.Sheets("Activity Data")
- Set wsVars = ThisWorkbook.Sheets("Variables")
- ' Store the Hour and Minute columns' indexes
- colIndexHour = 5
- colIndexMinute = 6
- ' Get current interval row
- currentIntervalRow = wsVars.Range("currentIntervalRow").Value
- 'Write selectedWeather, selectedVisibility, selectedVocalization, and selectedHeight in the Activity Data worksheet
- wsActivity.Cells(currentIntervalRow, 112).Value = selectedWeather
- wsActivity.Cells(currentIntervalRow, 7).Value = selectedVisibility
- wsActivity.Cells(currentIntervalRow, 20).Value = selectedVocalization
- wsActivity.Cells(currentIntervalRow, 19).Value = selectedHeight
- ' Move to next row and update values
- currentIntervalRow = currentIntervalRow + 1
- ' Read new hour & minute, and save it as currentIntervalHour and currentIntervalMinute
- currentIntervalHour = Format(wsActivity.Cells(currentIntervalRow, colIndexHour).Value, "00")
- currentIntervalMinute = Format(wsActivity.Cells(currentIntervalRow, colIndexMinute).Value, "00")
- ' Batch update the values of the variables stored in the Variables sheet
- ' We create an array filled with the values that we want to use for the update, before using it to update the corresponding range in the Variables sheet in one go
- ' Note that this works only because the values in the array are in the same consecutive order as the corresponding columns (E2 to S2) in the Variable sheets
- Dim resetValues As Variant
- resetValues = Array(currentIntervalHour, currentIntervalMinute, currentIntervalRow, vbNullString, _
- vbNullString, vbNullString, vbNullString, vbNullString, vbNullString, _
- vbNullString, vbNullString, vbNullString, vbNullString, "&H8000000D", "")
- wsVars.Range("E2:S2").Value = resetValues
- ' Update UI elements
- LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
- OptionButton0.BackColor = &HC0C0C0
- OptionButton1.BackColor = &HC0C0C0
- OptionButton2.BackColor = &HC0C0C0
- OptionButtonObsBreak.BackColor = &HFFFFC0
- ActivityButton.BackColor = &H8000000D
- ' Reset ComboBoxes and OptionButtons
- ComboBoxHeight.Value = vbNullString
- ComboBoxVocalization.Value = vbNullString
- ComboBoxWeather.Value = vbNullString
- OptionButton0.Value = False
- OptionButton1.Value = False
- OptionButton2.Value = False
- OptionButtonObsBreak.Value = False
- Set wsVars = Nothing
- Set wsActivity = Nothing
- Me.Hide ' Hide the form temporarily
- ' Save workbook
- ThisWorkbook.Save
- ' Show success message
- MsgBox "Interval saved successfully! The current interval is now " & currentIntervalHour & ":" & currentIntervalMinute, vbInformation
- activityDataHomeForm.Show
- End Sub
- Private Sub CommandButtonReviewActivityData_Click()
- Dim wsActivity As Worksheet
- Set wsActivity = ThisWorkbook.Sheets("Activity Data")
- wsActivity.Activate
- Set wsActivity = Nothing
- Unload Me
- backToPreviousFormForm.Show vbModeless
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement