VBA Loop to change label/text box object properties

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
333
Hi All,

Please could someone give me a hand. I'm not sure if this can be done the way I am trying to do it.

Basically, I have a userform with 40 labels on it (amongst other objects) and would like to update the BackColor property using a "For... Next" statement - instead of writing 40 lines of code... My thoughts are along the lines of the following code but I can't get it to work.

My labels are named "lbl_01" through to "lbl_40"

Code:
For t = 1 to 40
    "Userform1.lbl_" & format (t, "00") & ".BackColor = RGB(255, 0, 0)"
Next t

I'm using Excel 2016.

Any help will be greatly appreciated.

Thanks very much.
Ivan
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about
Code:
   For t = 1 To 40
     Me.Controls("lbl_" & Format(t, "00")).BackColor = RGB(255, 0, 0)
   Next t
 
Upvote 0
Solution
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top