Advertisement
Guest User

Untitled

a guest
Mar 31st, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.21 KB | None | 0 0
  1. Private Sub UserForm_Activate()
  2.  
  3. ' Store the name of the form in the currentForm global variable
  4.  
  5. currentFormName = Me.Name
  6.  
  7. ' Display the correct colors for the ActivityButton and PartyMembersButon using the corresponding global variables
  8.  
  9. If activityButtonColor = 0 Then
  10. activityButtonColor = &H8000000D 'Reset the global variable to the default color if it is null/has'nt been set yet(Blue)
  11. End If
  12. Me.ActivityButton.BackColor = activityButtonColor
  13.  
  14.  
  15. If partyMembersButtonColor = 0 Then
  16. partyMembersButtonColor = &H80FF& 'Reset the global variable to the default color if it is null/has'nt been set yet(Orange)
  17. End If
  18. Me.PartyMembersButton.BackColor = partyMembersButtonColor
  19.  
  20. 'Display the current interval time in the label, by combining the currentIntervalHour and currentIntervalMinute global variables
  21.  
  22. LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
  23.  
  24. ' Set the ComboBoxVocalization value to the global Vocalization variable, if it exists
  25. If selectedVocalization <> vbNullString Then
  26. ComboBoxVocalization.Value = selectedVocalization
  27. End If
  28.  
  29. ' Set the ComboBoxWeather value to the global Weather variable, if it exists
  30. If selectedWeather <> vbNullString Then
  31. ComboBoxWeather.Value = selectedWeather
  32. End If
  33.  
  34. ' Set the ComboBoxHeight value to the global Height variable, if it exists
  35. If selectedHeight <> vbNullString Then
  36. ComboBoxHeight.Value = selectedHeight
  37. End If
  38.  
  39. ' Set the OptionButton for visibility to selectedVisibility global variable, if it exists
  40. Select Case selectedVisibility
  41. Case "0"
  42. OptionButton0.Value = True
  43. Case "1"
  44. OptionButton1.Value = True
  45. Case "2"
  46. OptionButton2.Value = True
  47. Case "Obs.break"
  48. OptionButtonObsBreak.Value = True
  49. Case Else
  50. ' No option selected; reset all OptionButtons
  51. OptionButton0.Value = False
  52. OptionButton1.Value = False
  53. OptionButton2.Value = False
  54. OptionButtonObsBreak.Value = False
  55. End Select
  56.  
  57.  
  58. End Sub
  59.  
  60. Private Sub ComboBoxWeather_Change()
  61.  
  62. 'assign the selected value in ComboBoxWeather to the selectedWeather global variable
  63.  
  64. selectedWeather = ComboBoxWeather.Value
  65.  
  66. End Sub
  67.  
  68. Private Sub ComboBoxHeight_Change()
  69.  
  70. 'assign the selected value in ComboBoxHeight to the selectedHeight global variable
  71.  
  72. selectedHeight = ComboBoxHeight.Value
  73.  
  74. End Sub
  75.  
  76. Private Sub ComboBoxVocalization_Change()
  77.  
  78. 'assign the selected value in ComboBoxVocalization to the selectedVocalization global variable
  79.  
  80. selectedVocalization = ComboBoxVocalization.Value
  81.  
  82. End Sub
  83.  
  84. 'The 4 subs below make sure that the local visibility variable is assigned the value of the corresponding OptionButton;
  85. 'it also attributes a similar index value that we will use in the next sub
  86.  
  87. Private Sub OptionButton0_Click()
  88. HandleOptionButtonClick 0, "0"
  89. End Sub
  90.  
  91. Private Sub OptionButton1_Click()
  92. HandleOptionButtonClick 1, "1"
  93. End Sub
  94.  
  95. Private Sub OptionButton2_Click()
  96. HandleOptionButtonClick 2, "2"
  97. End Sub
  98.  
  99. Private Sub OptionButtonObsBreak_Click()
  100. HandleOptionButtonClick "ObsBreak", "Obs.break"
  101. End Sub
  102.  
  103. ' This sub handles color updates, so the selected OptionButton gets colored in green, and gets back to normal when unselected
  104. 'It also updates the local visibility variable so it corresponds to the selected button
  105.  
  106. Private Sub HandleOptionButtonClick(ByVal Index As Variant, ByVal visibility As String)
  107. 'Update the value of the local visibility variable
  108. HandleVisibilityChange visibility
  109.  
  110. ' Manually reset colors for all OptionButtons
  111. OptionButton0.BackColor = &HC0C0C0 ' Default color
  112. OptionButton1.BackColor = &HC0C0C0
  113. OptionButton2.BackColor = &HC0C0C0
  114. OptionButtonObsBreak.BackColor = &HFFFFC0
  115.  
  116. ' Change color of the selected OptionButton
  117. If IsNumeric(Index) Then
  118. Me.Controls("OptionButton" & Index).BackColor = RGB(0, 200, 100) ' Green
  119. Else
  120. 'as OptionButtonObsBreak does not have a numerical index, we need to handle it separately
  121.  
  122. Me.Controls("OptionButtonObsBreak").BackColor = RGB(0, 200, 100) ' Green
  123. End If
  124. End Sub
  125.  
  126. Private Sub HandleVisibilityChange(ByVal visibility As String)
  127. ' Update the selectedVisibility global variable, using the local visibility variable
  128. selectedVisibility = visibility
  129. End Sub
  130. Private Sub CommandButtonBackToIntervalForm_Click()
  131.  
  132. Unload Me
  133. intervalForm.Show
  134.  
  135. End Sub
  136.  
  137. Private Sub ActivityButton_Click()
  138.  
  139. Me.Hide
  140. mainActivityForm.Show
  141.  
  142. End Sub
  143.  
  144. Private Sub PartyMembersButton_Click()
  145.  
  146. Me.Hide
  147. PartyMemberForm.Show
  148.  
  149.  
  150.  
  151. End Sub
  152.  
  153.  
  154. Private Sub CommandButtonReviewActivityData_Click()
  155.  
  156. Dim wsActivity As Worksheet
  157. Set wsActivity = ThisWorkbook.Sheets("Activity Data")
  158.  
  159. wsActivity.Activate
  160.  
  161. Set wsActivity = Nothing
  162.  
  163. Unload Me
  164.  
  165. backToPreviousFormForm.Show vbModeless
  166.  
  167. End Sub
  168.  
  169. Private Sub CommandButtonComment_Click()
  170.  
  171. CommentForm.Show
  172.  
  173. End Sub
  174.  
  175.  
  176. Private Sub CommandButtonSocialNotes_Click()
  177.  
  178. socialNotesForm.Show
  179.  
  180. End Sub
  181.  
  182. Private Sub CommandButtonSaveAndNext_Click()
  183. Dim wsActivity As Worksheet
  184. Set wsActivity = ThisWorkbook.Sheets("Activity Data")
  185.  
  186.  
  187. ' Update worksheet values
  188. wsActivity.Cells(currentIntervalRow, 112).Value = selectedWeather
  189. wsActivity.Cells(currentIntervalRow, 7).Value = selectedVisibility
  190. wsActivity.Cells(currentIntervalRow, 20).Value = selectedVocalization
  191. wsActivity.Cells(currentIntervalRow, 19).Value = selectedHeight
  192.  
  193. ' Move to next row
  194. currentIntervalRow = currentIntervalRow + 1
  195. currentIntervalHour = Format(wsActivity.Cells(currentIntervalRow, 5).Value, "00")
  196. currentIntervalMinute = Format(wsActivity.Cells(currentIntervalRow, 6).Value, "00")
  197.  
  198. ' Update UI elements in a single batch
  199. LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
  200. OptionButton0.BackColor = &HC0C0C0
  201. OptionButton1.BackColor = &HC0C0C0
  202. OptionButton2.BackColor = &HC0C0C0
  203. OptionButtonObsBreak.BackColor = &HFFFFC0
  204. ActivityButton.BackColor = &H8000000D
  205. PartyMembersButton.BackColor = &H80FF&
  206.  
  207. activityButtonColor = &H8000000D 'Reset the global variable to Blue
  208. partyMembersButtonColor = &H80FF& 'Reset the global variable to Orange
  209.  
  210. ' Reset selected variables
  211. selectedWeather = vbNullString
  212. selectedHeight = vbNullString
  213. selectedVisibility = vbNullString
  214. selectedActivity = vbNullString
  215. selectedVocalization = vbNullString
  216. selectedItemFocal = vbNullString
  217. selectedItemDetail = vbNullString
  218. selectedJenisFocal = vbNullString
  219. selectedJenisLokasi = vbNullString
  220. focalCling = vbNullString
  221.  
  222. ' Reset ListPmDistance values efficiently
  223. Dim key As Variant
  224. For Each key In ListPmDistance
  225. ListPmDistance(key) = vbNullString
  226. Next key
  227.  
  228. ' Clear ComboBoxes and OptionButtons in one go
  229. ComboBoxHeight.Value = vbNullString
  230. ComboBoxVocalization.Value = vbNullString
  231. ComboBoxWeather.Value = vbNullString
  232. OptionButton0.Value = False
  233. OptionButton1.Value = False
  234. OptionButton2.Value = False
  235. OptionButtonObsBreak.Value = False
  236.  
  237. ' Save workbook if necessary
  238. ThisWorkbook.Save
  239.  
  240. Set wsActivity = Nothing
  241.  
  242. ' Show success message
  243. MsgBox "Interval saved successfully! The current interval is now " & currentIntervalHour & ":" & currentIntervalMinute, vbInformation
  244.  
  245.  
  246.  
  247. End Sub
  248.  
  249. Private Sub CommandButtonNextIntervalSame_Click()
  250. Dim wsActivity As Worksheet
  251. Dim colIndexHour As Long, colIndexMinute As Long
  252. Dim oldRow As Long, destRow As Long
  253. Dim key As Variant
  254.  
  255. ' Set worksheet reference
  256. Set wsActivity = ThisWorkbook.Sheets("Activity Data")
  257.  
  258.  
  259. ' Store the Hour and Minute columns' indexes in variables
  260. colIndexHour = 5
  261. colIndexMinute = 6
  262.  
  263. ' Store row indices
  264. oldRow = currentIntervalRow - 1
  265. destRow = currentIntervalRow
  266.  
  267. ' Copy only the desired continuous range (G to DI)
  268. wsActivity.Range("G" & oldRow & ":DI" & oldRow).Copy
  269. wsActivity.Range("G" & destRow).PasteSpecial Paste:=xlPasteValues
  270.  
  271. ' Cleanup clipboard
  272. Application.CutCopyMode = False
  273.  
  274. ' Update global variables
  275. currentIntervalRow = currentIntervalRow + 1
  276. currentIntervalHour = Format(wsActivity.Cells(currentIntervalRow, colIndexHour).Value, "00")
  277. currentIntervalMinute = Format(wsActivity.Cells(currentIntervalRow, colIndexMinute).Value, "00")
  278.  
  279. ' Update label
  280. LabelCurrentInterval.Caption = currentIntervalHour & ":" & currentIntervalMinute
  281.  
  282. ' Reset selected variables
  283. selectedWeather = vbNullString
  284. selectedHeight = vbNullString
  285. selectedVisibility = vbNullString
  286. selectedActivity = vbNullString
  287. selectedVocalization = vbNullString
  288. selectedItemFocal = vbNullString
  289. selectedItemDetail = vbNullString
  290. selectedJenisFocal = vbNullString
  291. selectedJenisLokasi = vbNullString
  292. focalCling = vbNullString
  293.  
  294. ' Reset dictionary values
  295. For Each key In ListPmDistance.Keys
  296. ListPmDistance(key) = vbNullString
  297. Next key
  298.  
  299. ' Reset colors for OptionButtons and Buttons
  300. OptionButton0.BackColor = &HC0C0C0
  301. OptionButton1.BackColor = &HC0C0C0
  302. OptionButton2.BackColor = &HC0C0C0
  303. OptionButtonObsBreak.BackColor = &HFFFFC0
  304. ActivityButton.BackColor = &H8000000D
  305. PartyMembersButton.BackColor = &H80FF&
  306. activityButtonColor = &H8000000D
  307. partyMembersButtonColor = &H80FF&
  308.  
  309. ' Clear ComboBoxes and OptionButtons
  310. ComboBoxHeight.Value = vbNullString
  311. ComboBoxVocalization.Value = vbNullString
  312. ComboBoxWeather.Value = vbNullString
  313.  
  314. OptionButton0.Value = False
  315. OptionButton1.Value = False
  316. OptionButton2.Value = False
  317. OptionButtonObsBreak.Value = False
  318.  
  319.  
  320. ' Save the workbook (so the observer does not have to do it manually, and we also do not overload the RAM of the tablet with unsaved data)
  321. ThisWorkbook.Save
  322.  
  323. Set wsActivity = Nothing
  324.  
  325. ' Show success message
  326. MsgBox "Interval saved successfully! The current interval is now " & currentIntervalHour & ":" & currentIntervalMinute, vbInformation
  327.  
  328.  
  329. End Sub
  330.  
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement