So, I have cleaned up my code (hopefully) and have gotten it to work......well, kind of.
I have a sheet with the various meds that are being taken by an individual (no confidential info here). I have a dynamic routine that "builds" the user form. I then have the option buttons in two groups of three. Those groups are dynamically named linking them, hopefully, to the med name in the original sheet.
I can get the routine to read and save the information for the main label (med name) and the time of day. But the other group for how often to give them is not being currently read in the right method. I cannot figure out why.
P.S. the routine reading the option buttons is something I got from another person.
A picture of my "input" form is included to see what I am trying to do.
Anyway, here is the code I am using
I have a sheet with the various meds that are being taken by an individual (no confidential info here). I have a dynamic routine that "builds" the user form. I then have the option buttons in two groups of three. Those groups are dynamically named linking them, hopefully, to the med name in the original sheet.
I can get the routine to read and save the information for the main label (med name) and the time of day. But the other group for how often to give them is not being currently read in the right method. I cannot figure out why.
P.S. the routine reading the option buttons is something I got from another person.
A picture of my "input" form is included to see what I am trying to do.
Anyway, here is the code I am using
VBA Code:
Dim i As Integer ' this make the variable not procedure specific.
Dim sGroupName$
Private Sub AddLabel_CommandButton_Click()
'==============================================================================
' adding in
'==============================================================================
Dim theLabel As Object
Dim labelCounter As Long
Worksheets("Sheet1").Select
Range("A3").Select
labelCounter = 1
iCounter = 3
optBtnCounter = 1
Do While ActiveCell.Value <> ""
Set theLabel = UserForm1.Controls.Add("Forms.Label.1", ActiveCell.Value, True)
With theLabel
.Caption = ActiveCell.Value
.Left = 10
.Width = 80
.Height = 15
.Top = 11 * labelCounter
.BorderStyle = fmBorderStyleSingle
.TabIndex = labelCounter + iCounter
End With
iCounter = iCounter + 1
thisGroupName = Left(ActiveCell.Value, InStr(1, ActiveCell.Value, " ") - 1) & Trim(Str(labelCounter))
Set OptBntAM = UserForm1.Controls.Add("Forms.OptionButton.1", OptionButton)
With OptBntAM
.Name = "OptionButton" & Trim(Str(optBtnCounter))
.Caption = "AM"
.Left = 100
.Top = 11 * labelCounter
.Height = 15
.GroupName = thisGroupName
Debug.Print "1 " & .Name
Debug.Print "1 " & .GroupName
If InStr(sGroupName, .GroupName) = 0 Then sGroupName = sGroupName & .GroupName & "|"
End With
optBtnCounter = optBtnCounter + 1
Set OptBntPM = UserForm1.Controls.Add("Forms.OptionButton.1", OptionButton)
With OptBntPM
.Name = "OptionButton" & Trim(Str(optBtnCounter))
.Caption = "PM"
.Left = 125
.Top = 11 * labelCounter
.Height = 15
.GroupName = thisGroupName
Debug.Print "1 " & .Name
Debug.Print "1 " & .GroupName
If InStr(sGroupName, .GroupName) = 0 Then sGroupName = sGroupName & .GroupName & "|"
End With
optBtnCounter = optBtnCounter + 1
Set OptBntAsNeeded = UserForm1.Controls.Add("Forms.OptionButton.1", OptionButton)
With OptBntAsNeeded
.Name = "OptionButton" & Trim(Str(optBtnCounter))
.Caption = "As Needed"
.Left = 150
.Top = 11 * labelCounter
.Height = 15
.GroupName = thisGroupName
Debug.Print "1 " & .Name
Debug.Print "1 " & .GroupName
If InStr(sGroupName, .GroupName) = 0 Then sGroupName = sGroupName & .GroupName & "|"
End With
optBtnCounter = optBtnCounter + 1
Set OptBtnEveryDay = UserForm1.Controls.Add("Forms.OptionButton.1", OptionButton)
With OptBtnEveryDay
.Name = "OptionButton" & Trim(Str(optBtnCounter))
.Caption = "Every Day"
.WordWrap = True
.Left = 220
.Width = 100
.Height = 15
.Top = 11 * labelCounter
.GroupName = Left(ActiveCell.Value, InStr(1, ActiveCell.Value, " ") - 1) & Trim(Str(labelCounter + 1))
Debug.Print "2 " & .Name
Debug.Print "2 " & .GroupName
End With
optBtnCounter = optBtnCounter + 1
Set OptBtnEveryOtherDay = UserForm1.Controls.Add("Forms.OptionButton.1", OptionButton)
With OptBtnEveryOtherDay
.Name = "OptionButton" & Trim(Str(optBtnCounter))
.Caption = "Every Other Day"
.WordWrap = True
.Left = 275
.Width = 100
.Height = 15
.Top = 11 * labelCounter
.GroupName = Left(ActiveCell.Value, InStr(1, ActiveCell.Value, " ") - 1) & Trim(Str(labelCounter + 1))
Debug.Print "2 " & .Name
Debug.Print "2 " & .GroupName
End With
optBtnCounter = optBtnCounter + 1
Set OptBtnSkipDays = UserForm1.Controls.Add("Forms.OptionButton.1", OptionButton)
With OptBtnSkipDays
.Name = "OptionButton" & Trim(Str(optBtnCounter))
.Caption = "Skip Day(s)"
.WordWrap = True
.Left = 355
.Width = 100
.Height = 15
.Top = 11 * labelCounter
.GroupName = Left(ActiveCell.Value, InStr(1, ActiveCell.Value, " ") - 1) & Trim(Str(labelCounter + 1))
Debug.Print "2 " & .Name
Debug.Print "2 " & .GroupName
End With
optBtnCounter = optBtnCounter + 1
ActiveCell.Offset(1, 0).Select
labelCounter = labelCounter + 2
Loop
End Sub
Private Sub Quit_CommandButton_Click()
Unload Me
End Sub
Private Sub Submit_CommandButton_click()
Dim arrGroups, arrData
Dim ctrl As Control
Dim b As Boolean
On Error GoTo errHandler
Debug.Print "Split sGroupName: " & sGroupName
arrGroups = Split(Left(sGroupName, Len(sGroupName) - 1), "|")
ReDim arrData(1 To UBound(arrGroups) + 1, 1 To UBound(arrGroups) + 1)
Debug.Print UBound(arrData)
For i = 0 To UBound(arrGroups)
b = False
For Each ctrl In UserForm1.Controls
Debug.Print ctrl.Name
Debug.Print "Tab: " & ctrl.TabIndex
Debug.Print "The control is a " & TypeName(ctrl)
If TypeName(ctrl) = "OptionButton" Then
Debug.Print ctrl.Value
Debug.Print "Button caption: " & ctrl.Caption
Debug.Print "Group Name: " & ctrl.GroupName
Debug.Print "Name: " & ctrl.Name
If ctrl.Value = True And ctrl.GroupName = arrGroups(i) Then
b = True
arrData(i + 1, 1) = ctrl.GroupName
arrData(i + 1, 2) = ctrl.Caption
Exit For
End If
End If
Next ctrl
If b = False Then Err.Raise vbobjecterr + 1000, , "Missing selections."
Next i
ActiveSheet.Range("A3").CurrentRegion.Offset(, 1).ClearContents
ActiveSheet.Range("B3").Resize(UBound(arrData, 1), 2).Value = arrData
Exit Sub
errHandler:
MsgBox Err.Description, vbCritical
End Sub