Object Qualifies

excelbytes

Board Regular
Joined
Dec 11, 2014
Messages
245
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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!
 
Upvote 0
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?
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,291
Members
448,564
Latest member
ED38

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