Getting Value from Option Buttons

PCWL

New Member
Joined
Jun 7, 2012
Messages
34
Hello,

I think this is a very simple question to answer and maybe I'm just missing something. I have created a userform with an OK button:

Code:
Private Sub cmdOK_Click()
    If optStraight = False And optBrick = False Then
        MsgBox "Please select a pattern"
    Else
        Unload frmPattern
    End If
End Sub
I have two options for the user to select a pattern, either "Straight" or "Brick", and a check to make sure that one of the patterns have been selected.

However, I'm having difficulty getting information from the userform to the code I need to use. I created a short macro to see what was going on and I got "False" values regardless of which option button I select.

Code:
        If frmPattern.optStraight.Value = True Then
            MsgBox "Straight"
        ElseIf frmPattern.optBrick.Value = True Then
            MsgBox "Brick"
        Else
            MsgBox frmPattern.optStraight.Value & " " & frmPattern.optBrick.Value
        End If
The output from this is "False False". Why can't I get "True" values?
Edit:
So I just thought of this right after I posted, but I think I'm losing the values when I use the "Unload" command. Is there another method of closing the userform without losing the information?
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It appears that you are unloading your Userform before your Second Sub Is Executed. Try changing your cmdok_click() to this
Code:
Private Sub cmdOK_Click()
    If optStraight = False And optBrick = False Then
        MsgBox "Please select a pattern"
    Else
       frmPattern.hide
    End If
End Sub
 
Upvote 0
Thanks for the quick reply bstory84.

I tried implementing that, but now none of the message boxes appear. Some calculations that occur after the message box still go through however, so it appears to be skipping the whole "If" statement.
 
Upvote 0
Can you post the complete code for the Sub after your cmdOK_click.
this one below.

Code:
   If frmPattern.optStraight.Value = True Then
            MsgBox "Straight"
        ElseIf frmPattern.optBrick.Value = True Then
            MsgBox "Brick"
        Else
            MsgBox frmPattern.optStraight.Value & " " & frmPattern.optBrick.Value
        End If
 
Upvote 0
This is the code someone else did before me, and I'm trying to add options into the code. I don't think the entire sub is necessary, as the userform is being put into just one case of a UCase.

I have removed calculations that were in the UCase, as they just modified certain cells on the spreadsheet (and I don't think my company would appreciate having their formulas online).

Here is the modified UCase:
Code:
    ElseIf UCase(frmJobInformation.cboGrilleType) = "GUARDIAN" Then
    
        frmPattern.Show
        
        If frmPattern.optStraight.Value = True Then
            MsgBox "Straight"
        ElseIf frmPattern.optBrick.Value = True Then
            MsgBox "Brick"
        Else
            MsgBox frmPattern.optStraight.Value & " " & frmPattern.optBrick.Value
        End If
        
        
        
        '********************************
        'ADD CODE TO CALCULTAE GLASS HERE
        '********************************
        glassComboArray() = GetGlassCombos(panelHeight)
        
        'CALCULATE THE QTY OF GLASS PER PANEL
        For i = 1 To 4
            glassComboArray(5).A = glassComboArray(5).A + glassComboArray(i).B
        Next i
        
        Application.ScreenUpdating = False 'To speed up the program
        
        'CALCULATE THE NUMBER AND SIZE OF PLAIN PANEL
        plainPanel(1, 1) = sumArray(plainPanelsPerSection): plainPanel(1, 2) = 4
        plainPanel(2, 1) = (glassComboArray(5).A - 1) * TotalPanels: plainPanel(2, 2) = 1
        plainPanel(3, 1) = TotalPanels: plainPanel(3, 2) = glassComboArray(5).B
        

        hangerPanel(1, 1) = sumArray(hangerPanelsPerSection)

        End With
 
Upvote 0
Try Stepping Through the code.
" ElseIf UCase(frmJobInformation.cboGrilleType) = "GUARDIAN" Then"
this must be testing as false for it to skip over the if statements.
 
Upvote 0
" ElseIf UCase(frmJobInformation.cboGrilleType) = "GUARDIAN" Then" works fine. When I run it, the command frmPattern.Show displays the userform. After "OK" is pressed, the "If" statements don't appear to trigger, but then the "glassComboArray() = GetGlassCombos(panelHeight)" triggers and the values in other cells on the spreadsheets are modified accordingly.
 
Upvote 0
Alright i'm a little confused.
Does the CmdOK_click() trigger this code or does the Code in question trigger the frmPattern UserForm?

If the code(refering to "the Code" because the actual sub name is not listed") is Triggering the frmPatter UserForm then the sub continues running after that it does not wait for the user to select a option button.
 
Upvote 0
It works like this:
Code:
Sub Code()
    UCase(frmJobInformation.cboGrilleType) = "GUARDIAN"
        frmPattern '(This is a box with two radial buttons to choose between brick and straight pattern, and an OK button)
            CmdOK_click() 'confirms an option was selected, closes frmPattern
        If statements 'want to utilize value from userform frmPattern to make calculations
        More calculations
    End With
End Sub
Edit:
Just a little bit more extra information. When I was using Unload frmPattern, it would run the "If" statements, just that it would be returning "False" values instead
It works, I was just being dumb. Thanks for all your help bstory84!
 
Last edited:
Upvote 0
Well i hope i have this right .
Changing it to this
Code:
 ElseIf UCase(frmJobInformation.cboGrilleType) = "GUARDIAN" Then
    
        frmPattern.Show 
Do Until frmPattern.Visible = False   ''''make sure your cmdok_click uses frmPattern.hide at the end of statement.

Application.Wait (Now + TimeSerial(0, 0, 1))
Loop

        
        If frmPattern.optStraight.Value = True Then
            MsgBox "Straight"
        ElseIf frmPattern.optBrick.Value = True Then
            MsgBox "Brick"
        Else
            MsgBox frmPattern.optStraight.Value & " " & frmPattern.optBrick.Value
        End If

  ''''Unload the frmPattern at the end of your sub.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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