pokemon_Man
pokemon_Man

Reputation: 902

How to create dynamic naming for each controls on userform in sub routine?

I want to create a routine that when called will perform the function base on the control names dynamically. Here's my code:

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

Upvotes: 0

Views: 42

Answers (1)

Kostas K.
Kostas K.

Reputation: 8508

You need to get the Control you want from the form's Controls collection:

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

Upvotes: 2

Related Questions