Hi guys,

I have some code which I know can be shortened by using a loop such as "Dim X as Integer" "from 1 to 50" "Next x", or something like that.

However, I have tried many variations of the code, and since I am a novice, I cannot get it to work!

This is a UserForm where I want to change the color of 'labels' based on whether the value in the spreadsheet in 1 or 0. Here is the code:

Sub x()
If Sheets("Data").Range("T1") = 1 Then 
    Me.Label1.BackColor = RGB(51, 102, 255) 
Else 
    Me.Label1.BackColor = RGB(79, 79, 79) 
End If 
 
 
If Sheets("Data").Range("T2") = 1 Then 
    Me.Label2.BackColor = RGB(51, 102, 255) 
Else 
    Me.Label2.BackColor = RGB(79, 79, 79) 
End If 
End Sub
My labels go from 1 to 50, as do the corresponding cells T1 to T50. Instead of copying and pasting this code fifty times, how I do I create a loop?

Any help would be greatly appreciated! Thanks.