Userform Initialise not chagneing forecolor

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a userform that when initialised I would like the forecolor changed if the value in a cell is not empty.

This would give a quick visual to indictae what cells are empty.

My code is this:

VBA Code:
Private Sub UserForm2_Initialize()

'If cellnot empty change label colour

With Sheet1
  If Range("T3").Value <> "" Then UserForm2.Opt1.ForeColor = RGB(0, 0, 255)
  If Range("U3").Value <> "" Then UserForm2.Opt2.ForeColor = RGB(0, 0, 255)
  If Range("V3").Value <> "" Then UserForm2.Opt3.ForeColor = RGB(0, 0, 255)
  If Range("W3").Value <> "" Then UserForm2.Opt4.ForeColor = RGB(0, 0, 255)
End With

UserForm2.Show
End Sub

However, in cells T3, U3, V3 & W3, the are formulas such as:

=IF(OR($P$2="No",T2=""),"",XLOOKUP(T2,$H$2:$H$25,$D$2:$D$25)), so T3 to W3, is not actually empty.

Can somebody please give me some guidance how to change the forecolor of the userform when the above formula equates to "".

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Your Initialize event is never executed, the name of the event is UserForm_Initialize regardless of the name of the userform.
 
Upvote 0
Solution
Your Initialize event is never executed, the name of the event is UserForm_Initialize regardless of the name of the userform.
Hi Norie, thanks for that. I changed the initialize and sure enough, the text now changes colour.

But now I have a Run-time error - 91, Object Variable or With block Variable not set showing up.

There aren't any variables in the code above.

What am I doing incorrectly ?

Thanks for your help.
 
Upvote 0
Found the issue. I had two UserForm2.show statements. On ein the userform code and one on the module that calle dthe userform.

Liver & Learn :)
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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