Advertisement
Guest User

Untitled

a guest
Mar 31st, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. 'Define variables to store the data selected by the observer, usable throughout the whole form
  2.  
  3. Private currentFormName As String
  4. Private activityButtonColor As String
  5. Private partyMembersButtonColor As String
  6. Private currentIntervalHour As String
  7. Private currentIntervalMinute As String
  8. Private currentIntervalRow As String
  9. Private selectedWeather As String
  10. Private selectedHeight As String
  11. Private selectedVisibility As String
  12. Private selectedActivity As String
  13. Private selectedVocalization As String
  14.  
  15.  
  16. Private Sub UserForm_Activate()
  17.  
  18. Dim wsVars As Worksheet
  19.  
  20. 'Define wsVars as the Variables worksheet
  21.  
  22. Set wsVars = ThisWorkbook.Sheets("Variables")
  23.  
  24. ' Store the name of the form in the worksheet
  25. currentFormName = Me.Name
  26. wsVars.Range("currentFormName").Value = currentFormName
  27.  
  28. ' 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)
  29. Dim currentVariables As Variant
  30. currentVariables = wsVars.Range("E2:S2").Value
  31.  
  32.  
  33. ' Assign values to local variables using the variables stored in the newly created array
  34. currentIntervalHour = currentVariables(1, 1)
  35. currentIntervalMinute = currentVariables(1, 2)
  36. selectedWeather = currentVariables(1, 4)
  37. selectedHeight = currentVariables(1, 5)
  38. selectedVisibility = currentVariables(1, 6)
  39. selectedVocalization = currentVariables(1, 8)
  40. activityButtonColor = currentVariables(1, 14)
  41. partyMembersButtonColor = currentVariables(1, 15)
  42.  
  43. ' If no color is set, reset to default
  44. If activityButtonColor = vbNullString Then
  45. activityButtonColor = &H8000000D
  46. wsVars.Range("activityButtonColor").Value = activityButtonColor
  47. End If
  48. Me.ActivityButton.BackColor = activityButtonColor
  49.  
  50. ' Update the label
  51. LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
  52.  
  53. ' Set ComboBoxes if values exist
  54. If selectedVocalization <> vbNullString Then ComboBoxVocalization.Value = selectedVocalization
  55. If selectedWeather <> vbNullString Then ComboBoxWeather.Value = selectedWeather
  56. If selectedHeight <> vbNullString Then ComboBoxHeight.Value = selectedHeight
  57.  
  58. ' Set OptionButtons for Visibility
  59. Select Case selectedVisibility
  60. Case "0": OptionButton0.Value = True
  61. Case "1": OptionButton1.Value = True
  62. Case "2": OptionButton2.Value = True
  63. Case "Obs.break": OptionButtonObsBreak.Value = True
  64. Case Else
  65. OptionButton0.Value = False
  66. OptionButton1.Value = False
  67. OptionButton2.Value = False
  68. OptionButtonObsBreak.Value = False
  69. End Select
  70.  
  71. Set wsVars = Nothing
  72.  
  73. End Sub
  74.  
  75. Private Sub ComboBoxWeather_Change()
  76.  
  77. Dim wsVars As Worksheet
  78.  
  79. 'Define wsVars as the Variables worksheet
  80.  
  81. Set wsVars = ThisWorkbook.Sheets("Variables")
  82.  
  83. 'assign the selected value in ComboBoxWeather to the selectedWeather variable
  84.  
  85. selectedWeather = ComboBoxWeather.Value
  86. wsVars.Range("selectedWeather").Value = selectedWeather
  87.  
  88. Set wsVars = Nothing
  89.  
  90. End Sub
  91.  
  92. Private Sub ComboBoxHeight_Change()
  93.  
  94.  
  95. Dim wsVars As Worksheet
  96.  
  97. 'Define wsVars as the Variables worksheet
  98.  
  99. Set wsVars = ThisWorkbook.Sheets("Variables")
  100.  
  101. 'assign the selected value in ComboBoxHeight to the selectedHeight variable
  102.  
  103. selectedHeight = ComboBoxHeight.Value
  104. wsVars.Range("selectedHeight").Value = selectedHeight
  105.  
  106. Set wsVars = Nothing
  107.  
  108. End Sub
  109.  
  110. Private Sub ComboBoxVocalization_Change()
  111.  
  112. Dim wsVars As Worksheet
  113.  
  114. 'Define wsVars as the Variables worksheet
  115.  
  116. Set wsVars = ThisWorkbook.Sheets("Variables")
  117.  
  118. 'assign the selected value in ComboBoxVocalization to the selectedVocalization variable
  119.  
  120. selectedVocalization = ComboBoxVocalization.Value
  121. wsVars.Range("selectedVocalization").Value = selectedVocalization
  122.  
  123. Set wsVars = Nothing
  124.  
  125. End Sub
  126.  
  127. 'The 4 subs below make sure that the local visibility variable is assigned the value of the corresponding OptionButton;
  128. 'it also attributes a similar index value that we will use in the next sub
  129.  
  130. Private Sub OptionButton0_Click()
  131. HandleOptionButtonClick 0, "0"
  132. End Sub
  133.  
  134. Private Sub OptionButton1_Click()
  135. HandleOptionButtonClick 1, "1"
  136. End Sub
  137.  
  138. Private Sub OptionButton2_Click()
  139. HandleOptionButtonClick 2, "2"
  140. End Sub
  141.  
  142. Private Sub OptionButtonObsBreak_Click()
  143. HandleOptionButtonClick "ObsBreak", "Obs.break"
  144. End Sub
  145.  
  146. ' This sub handles color updates, so the selected OptionButton gets colored in green, and gets back to normal when unselected
  147. 'It also updates the local visibility variable so it corresponds to the selected button
  148.  
  149. Private Sub HandleOptionButtonClick(ByVal Index As Variant, ByVal visibility As String)
  150. 'Update the value of the local visibility variable
  151. HandleVisibilityChange visibility
  152.  
  153. ' Manually reset colors for all OptionButtons
  154. OptionButton0.BackColor = &HC0C0C0 ' Default color
  155. OptionButton1.BackColor = &HC0C0C0
  156. OptionButton2.BackColor = &HC0C0C0
  157. OptionButtonObsBreak.BackColor = &HFFFFC0
  158.  
  159. ' Change color of the selected OptionButton
  160. If IsNumeric(Index) Then
  161. Me.Controls("OptionButton" & Index).BackColor = RGB(0, 200, 100) ' Green
  162. Else
  163. 'as OptionButtonObsBreak does not have a numerical index, we need to handle it separately
  164.  
  165. Me.Controls("OptionButtonObsBreak").BackColor = RGB(0, 200, 100) ' Green
  166. End If
  167. End Sub
  168.  
  169. Private Sub HandleVisibilityChange(ByVal visibility As String)
  170.  
  171. Dim wsVars As Worksheet
  172.  
  173. 'Define wsVars as the Variables worksheet
  174.  
  175. Set wsVars = ThisWorkbook.Sheets("Variables")
  176.  
  177. ' Update the selectedVisibility variable, using the local visibility variable
  178. selectedVisibility = visibility
  179. wsVars.Range("selectedVisibility").Value = selectedVisibility
  180.  
  181. Set wsVars = Nothing
  182.  
  183. End Sub
  184.  
  185. Private Sub CommandButtonSocialNotes_Click()
  186.  
  187. socialNotesForm.Show
  188.  
  189. End Sub
  190.  
  191. Private Sub CommandButtonSaveAndNext_Click()
  192. Dim colIndexHour As Long, colIndexMinute As Long
  193. Dim wsActivity As Worksheet
  194. Dim wsVars As Worksheet
  195. Dim dataArray As Variant
  196. Dim saveInterval As Integer
  197.  
  198. ' Define worksheets
  199. Set wsActivity = ThisWorkbook.Sheets("Activity Data")
  200. Set wsVars = ThisWorkbook.Sheets("Variables")
  201.  
  202. ' Store the Hour and Minute columns' indexes
  203. colIndexHour = 5
  204. colIndexMinute = 6
  205.  
  206. ' Get current interval row
  207. currentIntervalRow = wsVars.Range("currentIntervalRow").Value
  208.  
  209. 'Write selectedWeather, selectedVisibility, selectedVocalization, and selectedHeight in the Activity Data worksheet
  210. wsActivity.Cells(currentIntervalRow, 112).Value = selectedWeather
  211. wsActivity.Cells(currentIntervalRow, 7).Value = selectedVisibility
  212. wsActivity.Cells(currentIntervalRow, 20).Value = selectedVocalization
  213. wsActivity.Cells(currentIntervalRow, 19).Value = selectedHeight
  214.  
  215. ' Move to next row and update values
  216. currentIntervalRow = currentIntervalRow + 1
  217.  
  218. ' Read new hour & minute, and save it as currentIntervalHour and currentIntervalMinute
  219. currentIntervalHour = Format(wsActivity.Cells(currentIntervalRow, colIndexHour).Value, "00")
  220. currentIntervalMinute = Format(wsActivity.Cells(currentIntervalRow, colIndexMinute).Value, "00")
  221.  
  222. ' Batch update the values of the variables stored in the Variables sheet
  223. ' 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
  224. ' 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
  225. Dim resetValues As Variant
  226.  
  227. resetValues = Array(currentIntervalHour, currentIntervalMinute, currentIntervalRow, vbNullString, _
  228. vbNullString, vbNullString, vbNullString, vbNullString, vbNullString, _
  229. vbNullString, vbNullString, vbNullString, vbNullString, "&H8000000D", "")
  230.  
  231. wsVars.Range("E2:S2").Value = resetValues
  232.  
  233. ' Update UI elements
  234. LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
  235. OptionButton0.BackColor = &HC0C0C0
  236. OptionButton1.BackColor = &HC0C0C0
  237. OptionButton2.BackColor = &HC0C0C0
  238. OptionButtonObsBreak.BackColor = &HFFFFC0
  239. ActivityButton.BackColor = &H8000000D
  240.  
  241. ' Reset ComboBoxes and OptionButtons
  242. ComboBoxHeight.Value = vbNullString
  243. ComboBoxVocalization.Value = vbNullString
  244. ComboBoxWeather.Value = vbNullString
  245. OptionButton0.Value = False
  246. OptionButton1.Value = False
  247. OptionButton2.Value = False
  248. OptionButtonObsBreak.Value = False
  249.  
  250. Set wsVars = Nothing
  251. Set wsActivity = Nothing
  252.  
  253. Me.Hide ' Hide the form temporarily
  254.  
  255. ' Save workbook
  256.  
  257. ThisWorkbook.Save
  258.  
  259. ' Show success message
  260. MsgBox "Interval saved successfully! The current interval is now " & currentIntervalHour & ":" & currentIntervalMinute, vbInformation
  261.  
  262. activityDataHomeForm.Show
  263.  
  264.  
  265. End Sub
  266.  
  267. Private Sub CommandButtonReviewActivityData_Click()
  268.  
  269. Dim wsActivity As Worksheet
  270. Set wsActivity = ThisWorkbook.Sheets("Activity Data")
  271.  
  272. wsActivity.Activate
  273.  
  274. Set wsActivity = Nothing
  275.  
  276. Unload Me
  277.  
  278. backToPreviousFormForm.Show vbModeless
  279.  
  280. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement