Using VBA: Assign value to label control on userform

1.4k views Asked by At

I am working in the VBA code behind a userform. I have a string variable named block that corresponds to the name of a text label on my userform. After looking at other questions it seems using the controls function gives the ability to control the settings of a label on the userform. The block variable is a string of the labels name.I get an error at all attempts below

 Me.Controls(block).BackColor = &HFFFF&

When I try to hard code it to make sure it is the exact name of the label I still get an error

 Me.Controls("S111").BackColor = &HFFFF&

The following line DOES work:

 Me.S111.BackColor = &HFFFF&

When I try a similar technique I get another error with:

Me.block.BackColor = &HFFFF&

What am I doing wrong and is what I am doing possible?

1

There are 1 answers

0
Doug Detweiler On BEST ANSWER

The control Me.S111 is an object.

Dim objObject as Object '(Not type String)
Set objObject = Me.S111
objObject.BackColor = &HFFFF&