我有一个用户表单,其中包含4个问题,每个问题3个选项按钮。如果有人决定更改设计,我不希望他们必须编辑代码。是可能的,而不是我的循环是(1到3)和(1到4),我希望循环指定:
1 to # of frames
1 to # of questions in a frame
以下是我的代码:
'Transfer information
For j = 1 To 4 'columns 1 to 4 (4 questions)
For i = 1 To 3 '3 optionbutton in 1 question
labelstrr = "OptionButton" & labelnum 'concatenation of optionbutton1,2,...,12.
If Me.Controls(labelstrr).Value = True Then
Cells(emptyRow, j).Value = i
End If
labelnum = labelnum + 1
Next i
Next j
谢谢!
答案 0 :(得分:2)
您可以尝试此循环吗,我认为没有更快的方法。它会在消息框中返回值,但您可以将其分配给一个函数或调用一个子项,或者仅将其合并到其他代码中,具体取决于您正在执行的操作。
Sub xx()
Dim c As Control, n1 As Long, n2 As Long
For Each c In UserForm1.Controls
If TypeOf c Is msforms.OptionButton Then
n1 = n1 + 1
ElseIf TypeOf c Is msforms.Frame Then
n2 = n2 + 1
End If
Next c
MsgBox n1 & " option buttons in " & n2 & " frames (i.e. " & n1 / n2 & " questions per frame)"
End Sub