Use variable in UserForm Label Caption

Started by freshlychurnedbutter, February 13, 2022, 07:09:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

freshlychurnedbutter

I am trying to use variables in my label caption but I can't seem to figure out how to get this done. The variable, iTypeIO, is publicly declared in my module. Then I created a label called "Label1" in my userform. I left the caption as default "Label1". In the userform code section I enter this

Private Sub Label1_Click()
UserForm1.Label1.Caption = "Select Pin for " & iTypeIO
End Sub


However when I show the userform instead of seeing "Select Pin for "iTypeIO"", I still only see "Label1"

I went back to the label and this time inserted "Select Pin for " & iTypeIO in the caption field under Properties and now when I show the userform, I see the literal string "Select Pin for " & iTypeIO"

Any thoughts? Am I placing the string in the wrong location? Or is the userform somehow not getting passed the variable from the module even though I declared it as "Public" in my module? Thanks guys!


Paul Herber

Your label text is being set within a routine called Label1_Click.
You need to put your code in UserForm_Activate()

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

freshlychurnedbutter

Thanks Paul! I don't even know how much time I spent googling this. I would've never guessed to do that.

Paul Herber

That is the problem I have with VBA, a lot of it is just that, almost guesswork.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/