VBA Loop to change label/text box object properties

Ivan Howard

Active Member
Joined
Nov 10, 2004
Messages
323
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
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,423
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
   For t = 1 To 40
     Me.Controls("lbl_" & Format(t, "00")).BackColor = RGB(255, 0, 0)
   Next t
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,423
Office Version
  1. 365
Platform
  1. Windows
Ok, no rush. :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,423
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,109,159
Messages
5,527,149
Members
409,749
Latest member
esmarques

This Week's Hot Topics

Top