Checkbox click event triggered by VBA

Atnas

New Member
Joined
Aug 10, 2012
Messages
20
Hey Excel people

I have a problem.

I need to change the state of a checkbox, both from VBA code and from the sheet, but only have my code run when it's changed from the sheet.
My problem is that the click event is also triggered, when changing the state from VBA code.
I've tried using a MouseUp event, but I would like the user to be able to use a keyboard as well (and I can't change the state of the checkbox, because there is a Paste event in my code, that somehow stops the state from changing, weird, but not what I'm asking)

How would I go about solving my problem?

- Somehow detecting wether it was called from code?
- Making the mouseup event work?

Any help will be greatly appreciated.
 
If myCheck_Click is tested by pressing F5, then Application.Caller is an error value
If myCheck_Click is tested by assigning it to a button (or a checkbox) and clicking that control, it should be a string.

Yes, myCheck is the name of my checkbox. If you right-click the active x object and select view code, myCheck_click() is the sub routine. I added the code mentioned above from your example.

Then, when I click the checkbox using my mouse in the spreadsheet, the code returns "Error"... I tested this in a completely new spreadsheet with the same result.

New sheet --> Developer Tab --> Insert --> Check Box (Active X Control).
Draw box --> right-click --> view code --> add: MsgBox TypeName(Application.Caller)
Close code --> exit design mode --> click the box: Result = "Error".

This is using Excel 2013 on Windows 7
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
For anyone else in a similar situation as myself, I received some feedback from Jon Peltier, who suggested the code below. His code code appears to work:

"You could declare a public variable in the Declarations section of a regular code module:"
Code:
Public gbDidNotClick As Boolean
Then in your calling code:
Code:
Sub ViewCheckbox()
    gbDidNotClick = True
    Sheets(“Sheet1″).OLEObjects(“myCheckBox”).Object.Value = 1
    gbDidNotClick = False
End Sub
And finally, in the control event code:
Code:
Private Sub myCheckBox_Click()
    If Not gbDidNotClick Then
        MsgBox “Checkbox code is running.”
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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