我有一个用户表单,假设有三个框架,每个框架包含三个连续命名的选项按钮,意思是: Frame1 = OptionButton 1,2,3/ Frame2 = OptionButton 4,5,6/ Frame3 = OptionButton 7,8,9
我正在尝试创建一个for循环来搜索每个帧,检查选项按钮是否为真,如果是真的,则将该特定选项按钮的标题写入一个公共变量。
有人能帮帮我吗?!
编辑:
到目前为止我的代码
For i = 1 To 7
For z = 1 To 20
If UserForm1.Controls("Frame" & i).Caption = "Amputationstyp" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Amputationstyp = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
If UserForm1.Controls("Frame" & i).Caption = "Amputationsseite" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Amputationsseite = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
If UserForm1.Controls("Frame" & i).Caption = "Restgliedstabilität" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Restgliedstabilität = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
If UserForm1.Controls("Frame" & i).Caption = "Restgliedform" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Restgliedform = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
If UserForm1.Controls("Frame" & i).Caption = "Knochenauswüchse" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Knochenauswüchse = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
If UserForm1.Controls("Frame" & i).Caption = "Hautkrankheiten (am Stumpf)" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Hautkrankheiten = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
If UserForm1.Controls("Frame" & i).Caption = "Muskeltonus" Then
If UserForm1.Controls("OptionButton" & z).Value = True Then
Muskeltonus = UserForm1.Controls("OptionButton" & z).Caption
End If
End If
Next
Next问题:我试图检查帧标题,只有在满足这个条件时才在变量中写一些东西,但当选项按钮三被选中时,也满足了这个条件,这会导致所有变量中的值都是错误的。第一个if语句不能正常工作。
发布于 2020-08-05 04:02:00
您可以创建一个函数,该函数接受帧标题并返回该帧中所选选项的标题:
Private Sub Tester()
Amputationstyp = FrameOptionFromCaption("Amputationstyp")
Amputationsseite = FrameOptionFromCaption("Amputationsseite")
End Sub
'get the selected optionbutton caption from inside a frame
' with the provided caption
Function FrameOptionFromCaption(capt As String) As String
Dim c As Control, opt As Control
'loop all controls in the form (inside the Form code module "Me" = the form)
For Each c In Me.Controls
'is this a Frame?
If TypeName(c) = "Frame" Then
'does the frame's caption match the one provided?
If c.Caption = capt Then
For Each opt In c.Controls
'if the frame has other types of controls you should
' add a test to only look at optionbuttons...
If opt.Value = True Then
FrameOptionFromCaption = opt.Caption
Exit Function 'done searching
End If
Next opt
End If
End If
Next c
End Functionhttps://stackoverflow.com/questions/63251654
复制相似问题