Jon Chan
Jon Chan

Reputation: 53

VBA Function to Hide Userform Controls

I have 10 UserForm Controls with Names Account1 to Account10 that are defaulted as Visible = False. I am trying to create a Function to unhide the Controls in ascending numeric order.

Private Sub AddButton_Click()
Select Case CountTextBox.Value
Case 1
    Account1.Visible = True
Case 2
    Account2.Visible = True
Case 3
    Account3.Visible = True
Case 4
    Account4.Visible = True
Case 5
    Account5.Visible = True
Case 6
    Account6.Visible = True
Case 7
    Account7.Visible = True
Case 8
    Account8.Visible = True
Case 9
    Account9.Visible = True
Case 10
    Account10.Visible = True
End Select
End Sub

How can I simplify this VBA code?

Upvotes: 1

Views: 2240

Answers (1)

Jon Chan
Jon Chan

Reputation: 53

Adding to @Comintern's answer, solution is:

For i = 1 To 10
    Me.Controls("Account" & i).Visible = True
Next i

Upvotes: 1

Related Questions