Search code examples
excelexcel-formulaexcel-2010excel-2007vba

VBA - Value of an option button in a frame (within an Excel sheet)


I'm having problems with shapes, frames and option buttons... I'm a total newbie, I've never used them. I just put several option buttons on an Excel sheet (Using the FORM toolbox).

I'm trying to check whether my optionbutton is filled or not. So far, I've done the following :

Sub SX_EXTERNE()

Dim Ws As Worksheet
Dim ConBut As Shape
Dim Answer As String

Set Ws = ThisWorkbook.Sheets("Externe")

For Each ConBut In Ws.Shapes
    If ConBut.Type = msoFormControl Then
        If ConBut.FormControlType = xlOptionButton Then
            If ConBut.ControlFormat.Value = xlOn Then
                 Answer = ConBut.Name
            End If
        End If
    End If
Next ConBut

MsgBox Answer

End Sub

My problem is I do not know how to check only in a selected frame (i.e. "Conges_generaux" for my example):

enter image description here

enter image description here

enter image description here

Could you please give me a hint? I've seen many subjects about that but many of them treat of ActiveXControls... I don't even know the difference.

Thanks


Solution

  • Here is a quick way

    Sub Sample()
        Dim optBtn As OptionButton
    
        For Each optBtn In ActiveSheet.OptionButtons
            If optBtn.Value = 1 Then
               Debug.Print optBtn.Name
               Debug.Print optBtn.GroupBox.Name
            End If
        Next
    End Sub
    

    So in your code change Dim ConBut As Shape to Dim ConBut As OptionButton. Feel free to put relevant checks and store it in the relevant answer variable :)