如何为子例程中的userform上的每个控件创建动态命名?

时间:2017-04-28 15:57:21

标签: vba

我想创建一个例程,在调用时将动态地在控件名称上执行函数。这是我的代码:

Private Sub myControl(counter as string)
    If mySecondControl & counter.Value = "Y" Then
        myThirdControl & counter.Caption = "Do Something Here" 'Error syntax
    End If
End Sub

Private Sub doThis_Change()
    myControl("1")
    myControl("2")
End Sub

1 个答案:

答案 0 :(得分:2)

您需要从表单的Control集合中获取所需的Controls

If Controls("mySecondControl" & counter).Value = "Y" Then
    Controls("myThirdControl" & counter).Caption = "Do Something Here"
End If