0

I have a button on my userform, lets call it CommandButton2. I have code that outputs at the very end a long value of 2 to refer directly to the button lets call that long ButtonValue.

Now if I wanted to loop through a number of buttons using the long value my first thought would be to do something like CommandButton(ButtonValue).Caption = Value, which obviously doesn't work.

I can use something like string concatenation to produce a string output CommandButton2.Caption but then how do I actually get that string to reference the UserForm object?

Am I even able to do such a thing and still make use of the .Caption object property? Any help would be appreciated.

0

1 Answer 1

2

Following should work for you. Use Controls collection to set property of an object from variable.

Private Sub CommandButton1_Click()
Dim ButtonValue As Long
    ButtonValue = 2
    Controls("CommandButton" & ButtonValue).Caption = ButtonValue
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, Controls is what I was missing there, thank you
You should use Me.Controls.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.