Results 1 to 4 of 4

Thread: Object Qualifies
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Dec 2014
    Posts
    13
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Object Qualifies

    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

  2. #2
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,855
    Post Thanks / Like
    Mentioned
    35 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Object Qualifies

    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!

  3. #3
    New Member
    Join Date
    Dec 2014
    Posts
    13
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Object Qualifies

    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?

  4. #4
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,855
    Post Thanks / Like
    Mentioned
    35 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Object Qualifies

    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:
    Worksheets("Sheet1").TextBox1.BackColor = RGB(255,0,0))
    Last edited by Domenic; Aug 14th, 2019 at 08:21 PM.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •