首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建for-loop问题

创建for-loop问题
EN

Stack Overflow用户
提问于 2020-08-05 00:57:38
回答 1查看 34关注 0票数 0

我有一个用户表单,假设有三个框架,每个框架包含三个连续命名的选项按钮,意思是: Frame1 = OptionButton 1,2,3/ Frame2 = OptionButton 4,5,6/ Frame3 = OptionButton 7,8,9

我正在尝试创建一个for循环来搜索每个帧,检查选项按钮是否为真,如果是真的,则将该特定选项按钮的标题写入一个公共变量。

有人能帮帮我吗?!

编辑:

到目前为止我的代码

代码语言:javascript
复制
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语句不能正常工作。

EN

回答 1

Stack Overflow用户

发布于 2020-08-05 04:02:00

您可以创建一个函数,该函数接受帧标题并返回该帧中所选选项的标题:

代码语言:javascript
复制
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 Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63251654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档