Returning an index value from a group of ActiveX Option Buttons

后端 未结 1 535
醉话见心
醉话见心 2021-01-28 04:16

Let me start off by stating that I am a medical student, not a programmer. I have written a spreadsheet that will work as an exam with 50 questions. Each question has 15 multi

相关标签:
1条回答
  • 2021-01-28 05:18

    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
    
    0 讨论(0)
提交回复
热议问题