0 votes
in VBA by Beginner (5 points)

I need to create several check boxes using VBA and I'm using the code below to test as I develop the feature.

Everything works as written except "BackColor" (in the example) and I can't figure out why.

The message is "Object doesn't support this property or method".

Can you please help? Thank you!
 

Sub addCheckBox()
    Sheets("Sheet1").CheckBoxes.Add(Left:=Range("B2").Left, Top:=Range("B2").Top, Width:=Range("C2").Width, Height:=Range("B2").Height).Select
    With Selection
        .Caption = "Test"
        .LinkedCell = "Sheet1!$C$2"
        .BackColor = RGB(200, 200, 200)
    End With
End Sub

 

2 Answers

0 votes
by Expert (911 points)
selected by
 
Best answer

Change .BackColor to .Interior.Color

by Beginner (5 points)
That worked! Thank you.

Any idea of why BackColor does not work? Is it an obsolete keyword?
by Super Expert (3.2k points)
The .BackColor property only works for ActiveX and UserForm CheckBoxes. The CheckBoxes created with your macro are Form Control CheckBoxes so properties are handled a little differently.
0 votes
by Super Expert (3.2k points)

The .BackColor property only works for ActiveX and UserForm CheckBoxes. Since you're making a Form Control CheckBox, you'll need to set the .Interior.Color property, like this:

Sub addCheckBox()
    Sheets("Sheet1").CheckBoxes.Add(Left:=Range("B2").Left, Top:=Range("B2").Top, Width:=Range("C2").Width, Height:=Range("B2").Height).Select
    With Selection
        .Caption = "Test"
        .LinkedCell = "Sheet1!$C$2"
        .Interior.Color = RGB(200, 200, 200)
    End With
End Sub

We have a good tutorial for how to change the background color of checkboxes if you're interested in learning more about the different ways to change the fill color of different checkbox types.

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...