3

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?

6
  • I assume that block variable stores value "S111". Am i right? Commented Jun 19, 2015 at 20:52
  • 2
    Me.Controls("S111").BackColor = &HFFFF& works for me. Commented Jun 19, 2015 at 21:06
  • 2
    Me.Controls(block).BackColor = &HFFFF& also works for me Commented Jun 19, 2015 at 21:09
  • 3
    What is the error message that you get? Commented Jun 19, 2015 at 21:15
  • 1
    Is the code actually in the userform's code module? Otherwise the Me keyword will not be referring to the user form and will therefore cause an error. Commented Jun 19, 2015 at 22:01

1 Answer 1

1

The control Me.S111 is an object.

Dim objObject as Object '(Not type String)
Set objObject = Me.S111
objObject.BackColor = &HFFFF&
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.