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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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