How to get optionbutton values from two different groups

fredep

New Member
Joined
Jan 13, 2024
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
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.
Med_Userform.jpg


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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You've had 55 views but no responses before this one and I'm wondering if that's because the issue is not clear and there's no question to answer. I think this might be too vague
But the other group for how often to give them is not being currently read in the right method.
Aside from that I have to wonder why you don't create the form manually rather than write all that code to do it. I tried playing a bit with that code and although the active cell has a value, no label shows up on the form so it all seems a bit elusive to me. Perhaps if you define the problem and ask specific questions you'll get more feedback.
 
Upvote 0
After seeing your posting here from a couple of weeks ago
I downloading your workbook and played around with it, came up with this
VBA Code:
' this make the variable not procedure specific.
    Dim sGroupName As String
'


Private Sub AddLabel_CommandButton_Click()

'==============================================================================
' adding in
'==============================================================================
    Dim theLabel As Object
    Dim labelCounter As Long
    Dim iCounter As Long
    Dim optBtnCounter As Long
    
    labelCounter = 1
    iCounter = 3
    optBtnCounter = 1

    Worksheets("Sheet1").Select
    Range("A3").Select

    
    Do While ActiveCell.Value <> ""
        '==============================================================================
        ' set up the lable
        '==============================================================================
        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
        
        '==============================================================================
        ' the first 3 option button group
        '==============================================================================
        thisgroupname = Left(ActiveCell.Value, InStr(1, ActiveCell.Value, " ") - 1) & "1"
        'Debug.Print "New group name: " & thisgroupname
        
        ' build the sGroupName string
        If InStr(sGroupName, Left(thisgroupname, Len(thisgroupname) - 1)) = 0 Then
            sGroupName = sGroupName & Left(thisgroupname, Len(thisgroupname) - 1) & "|"
        End If
    
        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 "Just created  " & .Name & " in Group " & .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 "Just created  " & .Name & " in Group " & .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
            .Value = True
            'Debug.Print "Just created  " & .Name & " in Group " & .GroupName
        End With
        optBtnCounter = optBtnCounter + 1
        
        '==============================================================================
        ' the second 3 option button group
        '==============================================================================
        thisgroupname = Left(ActiveCell.Value, InStr(1, ActiveCell.Value, " ") - 1) & "2"
        'Debug.Print "New group name: " & thisgroupname
        
        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 = thisgroupname
            'Debug.Print "Just created  " & .Name & " in Group " & .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 = thisgroupname
                'Debug.Print "Just created  " & .Name & " in Group " & .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 = thisgroupname
                .Value = True
                'Debug.Print "Just created  " & .Name & " in Group " & .GroupName
            End With
            optBtnCounter = optBtnCounter + 1
    
        ActiveCell.Offset(1, 0).Select
        labelCounter = labelCounter + 2
        
    Loop

    'Debug.Print "The group name string for splitting" & vbCrLf & sGroupName

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
    Dim i As Long
    
    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)
    
    For i = LBound(arrGroups) To UBound(arrGroups)
        b = False
        For Each ctrl In UserForm1.Controls
            If TypeName(ctrl) = "OptionButton" Then
                If ctrl.Value = True Then
                    ' the when part
                    If ctrl.GroupName = arrGroups(i) & "1" Then
                        b = True
                        arrData(i + 1, 1) = ctrl.Caption
                    End If
                    ' the daily part
                    If ctrl.GroupName = arrGroups(i) & "2" Then
                        b = True
                        arrData(i + 1, 2) = ctrl.Caption
                    End If
                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
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,084
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top