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 Skilled (466 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 (2.9k 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 (2.9k 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
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

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

...