Change userform textbox colours dynamically

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to find a way to change the background colour of textboxes on a userform to assist users who have dyslexia.

I thought it might be as simple as setting their preferred colour on installation of the file, then calling that colour each time the form is loaded, but it doesn't work.

This is what I have so far:

Code:
Public Yellow As LongPublic Gray As Long
Public White As Long
Sub SetColours()
    If Sheet2.Range("B11").Value = "Yellow" Then
        BColor = &H80FFFF
    End If
    If Sheet2.Range("B11").Value = "White" Then
        BColor = &H80000005
    End If
    'Yellow = &H80FFFF
    'Gray = &H8000000F
    'White = &H80000005
End Sub

I'm then trying to set the colour like this:

Code:
Private Sub UserForm_Initialize()
SetColours
TextAgg.BackColor = BColor

Now what should happen, if my theory was correct, is that the textbox should have a yellow background but it doesn't, it's black!

Can anyone help me please?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You haven't declared Bcolor anywhere, and it needs to be a string
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Appreciate you may have resolved your issue but you probably can avoid using Public variables & multiple If statements in your code by creating a simple function to return the desired backcolor

try following (standard module or forms code page)

Code:
 Function SetColors()   
   Dim m As Variant
    'Yellow = &H80FFFF
    'Gray = &H8000000F
    'White = &H80000005
    m = Application.Match(Sheet2.Range("B11").Value, Array("White", "Yellow", "Gray"), 0)
    If IsError(m) Then m = 1
    SetColors = Choose(m, &H80000005, &H80FFFF, &H8000000F)
End Function

If further choices are needed just add to the array & Choose selection as required


To call

Code:
 Private Sub UserForm_Initialize() 
  Me.TextAgg.BackColor = SetColors
End Sub

Dave
 
Last edited:
Upvote 0
Dave,

Thanks - your suggestion works so much better than mine!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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