Question:
Excel VBA - Change background colour of a label on a user-form?
Azim1983
2010-09-24 07:18:00 UTC
I am using a user-form to input data into a worksheet. The user-form contains various labels and I want to change the background colour of the label (so it matches that of the form). The colour that I want to use does not appear in the background colour list on the colour palette, therefore I need use the code to change the background colour of the labels.

I am using the "userform.BackColor = RGB(51, 102, 255)" to set the background colour of the form for when the form loads. I know that there is a similar method that I can use to set the background colour of my labels for when the form loads.

Can someone help?
Five answers:
garbo7441
2010-09-24 07:33:43 UTC
Use the Label Name:



Me.Label1.BackColor = RGB(51, 102, 255)



or



Me.Label_Title.BackColor = RGB(51, 102, 255)



etc



Iterate through all labels.
devilishblueyes
2010-09-27 16:00:32 UTC
Garbo is right on. The userform is referred to as "Me". If you want to keep the process simple for changing the background color of the labels then I recommend naming them about all the same. For example name them:



Label1, Label2, Label3, Label4, etc.



Just make sure you don't miss a number.



This makes it so you don't have to write tons of code to change the back color and instead you can do it something like this.



Dim x As Integer



For x = 1 To 22



Me.Controls("Label" & x).BackColor = RGB(51, 102, 255)



Next x





This is a lot simpler than having to write



Me.Label1.BackColor = RGB(51, 102, 255)



... all the way to...



Me.Label22.BackColor = RGB(51, 102, 255)







I'd rather write 4 simple lines of code instead of 22 lines of code. Plus, I don't have to change 22 lines if I ever decide I want it a different color. I only have to change 1 line instead.
?
2016-11-09 12:18:36 UTC
Excel Vba Background Color
?
2010-09-24 10:47:05 UTC
It's been awhile now but I think there is a .backgroundcolor for labels. If it doesn't accept RGB colors then you'll have to find the hex color codes (FFAA11) for the RGB colors.
Erika
2016-10-05 04:22:16 UTC
make your drop down checklist... then go format > Conditional Formatting. so. you may choose for B2 or which ever one you go with for to alter...then (interior the conditional formatting show screen) choose for If A1 is comparable to "organic gasoline" then exchange the format coloration to gray :) desire this facilitates.. Paul


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...