【问题标题】:Creating for-loop issue创建for循环问题
【发布时间】:2023-03-26 19:22:02
【问题描述】:

我有一个用户表单,可以说三个框架,每个框架包含三个选项按钮,它们的名称连续命名含义: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

The problem: I tried to check for the frame caption and only write something in the variable when this condition is met, but the condition is also met, when Option button three is getting checked which then leads to a wrong values in all的变量。第一个 if 语句没有按原样工作。

【问题讨论】:

  • 您遇到的具体问题是什么?运行代码时会发生什么?
  • Debug.Print Me.Controls("OptionButton" & ((i - 1) * 3) + n).Value 其中i 是框架编号,n 是框架内的选项按钮编号
  • 好吧,这和你最初的描述有点不同...

标签: vba loops for-loop userform option


【解决方案1】:

您可以创建一个函数,该函数采用框架标题并返回该框架内所选选项的标题:

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

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2021-06-12
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多