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

我的未来我决定 提交于 2020-01-03 02:40:14

问题


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):

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


回答1:


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 :)



来源:https://stackoverflow.com/questions/44454481/vba-value-of-an-option-button-in-a-frame-within-an-excel-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!