Returning an index value from a group of ActiveX Option Buttons

☆樱花仙子☆ 提交于 2019-12-02 09:33:19

The way to create a group event is to have a custom class to wrap the controls that you want to group and a module level collection to keep the wrapper class references alive.

After reviewing your workbook I determined that you can derive an index based on the OptionButton's Name.

Class: OptionWrapper

Option Explicit

Public WithEvents MyOptionButton As MSForms.OptionButton

Private Sub MyOptionButton_Click()
    Dim Letters()
    Dim lRow As Long, lAnswer As Long, ID As Long
    Letters = Array("O", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")

    ID = Replace(MyOptionButton.Name, "OptionButton", "")

    lRow = Int(ID / 15 + 1) * 21
    lAnswer = ID Mod 15

    Cells(lRow, "B") = Letters(lAnswer)

End Sub

Exam Worksheet Code Module

Private OptionsCollection As Collection

Private Sub Worksheet_Activate()
    Dim obj As OLEObject
    Dim wrap As OptionWrapper

    Set OptionsCollection = New Collection

    For Each obj In ActiveSheet.OLEObjects

        If TypeOf obj.Object Is MSForms.OptionButton Then
            Set wrap = New OptionWrapper
            Set wrap.MyOptionButton = obj.Object
            OptionsCollection.Add wrap
        End If

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