Object Qualifies

excelbytes

New Member
Joined
Dec 11, 2014
Messages
13
I am new to VBA. I am trying to change the background color of a text box based on another cell value. I am getting a Runtime error 424 Object required error. Here is my code - what am I doing wrong?

Public Sub TextBoxColor()


If Worksheets("Sheet1").Range("E1").Value = "Name" Then
TextBox1.Interior.ColorIndex = 3
ElseIf Worksheets("Sheet1").Range("E1").Value = "Blog" Then
TextBox1.Interior.ColorIndex = 10
ElseIf Worksheets("Sheet1").Range("E1").Value = "Help" Then
TextBox1.Interior.ColorIndex = 20
End If


End Sub
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
18,918
It looks like you have an ActiveX control that's located on a worksheet. If this is correct, you'll need to qualify your sheet reference. Also, you'll need to use the BackColor property of the TextBox object to set the background color. And, the color can be specified using any integer that represents a valid color, or you can use the RGB function. So, for example, let's say that Sheet1 contains your textbox, try...

Code:
worksheets("Sheet1").TextBox1.BackColor = RGB(255,0,0) 'red
Alternatively, you can refer to your sheet using its code name. So, for example, if you haven't changed it, the code name for Sheet1 would also be Sheet1. So you could do the following...

Code:
sheet1.TextBox1.BackColor = RGB(255,0,0)
Note that there are a some advantages to using the code name instead of the sheet name. First, if you change the name of the sheet, the code name will remain the same and there would be no need to amend the code. Secondly, once you type the code name and a dot (.), you'll get the benefit of the InteliSense. So you'll see a list of all the available properties and methods.

Hope this helps!
 

excelbytes

New Member
Joined
Dec 11, 2014
Messages
13
Thanks for the feedback. I have a data validation drop down box that chooses "Name", "Blog", or "Help". As I mentioned I am new to VBA so I'm not clear on what you mean by "you'll need to qualify your sheet reference". How do I do that?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
18,918
I just meant that you need to add a reference to the sheet containing the textbox, as I've shown in my example, like this...

Code:
[COLOR=#ff0000]Worksheets("Sheet1").[/COLOR]TextBox1.BackColor = RGB(255,0,0))
 
Last edited:

Forum statistics

Threads
1,077,686
Messages
5,335,656
Members
399,032
Latest member
thefinu

Some videos you may like

This Week's Hot Topics

Top