Command button to create checkbox on Excel Sheet

GalaxyFlip4

New Member
Joined
Oct 16, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am attempting to create a checkbox on an Excel worksheet when a command button is pressed and clear the checkbox caption.

I get an error message: "Compile Error - Invalid or unqualified reference" and the .Left is highlighted in this line: sh.CheckBoxes.Add(.Left + 1, .Top + 15, .Width - 1, .Height - 1).Select

Below is the code:
**********************************************
Private Sub CommandButton1_Click()

Dim lastRow As Double
Dim sh As Worksheet


Set sh = Sheets("data")
lastRow = sh.Range("A" & Rows.Count).End(xlUp).Row + 1

With sh.Range("E" & lastRow)
End With



sh.CheckBoxes.Add(.Left + 1, .Top + 15, .Width - 1, .Height - 1).Select
With Selection
.Caption = ""
'ChkBox.Caption = ""
End With

End Sub
*********************************************
Any ideas on how to fix?

Thank you all very much and be well.

Dion
<>
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You'll need to move that part of the code that adds your checkbox within With sh.Range("E" & lastRow)/End With.

By the way, you can avoid selecting your checkbox before setting the Caption.

Also, lastRow can be declared as Long.

Also, you should qualify Rows.Count with .RowsCount.

So your macro can be re-written as follows...

VBA Code:
Private Sub CommandButton1_Click()

    Dim lastRow As Long
    Dim sh As Worksheet
  
    Set sh = Sheets("data")
  
    With sh
        lastRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    End With
  
    With sh.Range("E" & lastRow)
        With sh.CheckBoxes.Add(.Left + 1, .Top + 15, .Width - 1, .Height - 1)
            .Caption = ""
        End With
    End With

End Sub

Hope this helps!
 
Upvote 0
You'll need to move that part of the code that adds your checkbox within With sh.Range("E" & lastRow)/End With.

By the way, you can avoid selecting your checkbox before setting the Caption.

Also, lastRow can be declared as Long.

Also, you should qualify Rows.Count with .RowsCount.

So your macro can be re-written as follows...

VBA Code:
Private Sub CommandButton1_Click()

    Dim lastRow As Long
    Dim sh As Worksheet
 
    Set sh = Sheets("data")
 
    With sh
        lastRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    End With
 
    With sh.Range("E" & lastRow)
        With sh.CheckBoxes.Add(.Left + 1, .Top + 15, .Width - 1, .Height - 1)
            .Caption = ""
        End With
    End With

End Sub

Hope this helps!

Thank you Dominic!

Very helpful and have a great day.

Dion
<>
 
Upvote 0
You're very welcome, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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