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.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Atnas,

One option would be to use a Public Variable as a flag to that gets set to True right before the VBA code changes the Checkbox, and is reset to false by the Checkbox_Click.

Macro code example. (the Public Variable needs to be declared before any procedure code).
Code:
Public bCheckBoxCalledFromCode

Sub MyMacro()
    bCheckBoxCalledFromCode = True
    Sheets("Sheet1").CheckBox1 = True
End Sub

ActiveX Checkbox_Click code example
Code:
Private Sub CheckBox1_Click()
    If bCheckBoxCalledFromCode Then
        bCheckBoxCalledFromCode = False
        MsgBox "Checked by code"
    Else
        MsgBox "User Checked " & IIf(Me.CheckBox1.Value, "On", "Off")
    End If
End Sub
 
Upvote 0
Hi bs0d,

After reading the other thread you referenced and your question here, your ultimate objective isn't clear to me.

It sounds like you want to be able to set the checkbox to either True or False through code and that for some reason just setting the Value property of the checkbox isn't working.

Typically, with no conflicting code in your worksheet, you should be able to simply set the Value property to True or False. I suspect you have code in your Checkbox_Click, Checkbox_Change event or elsewhere that is triggering and changing the state of the Checkbox.Value property. Have you tried placing a Breakpoint then stepping through the execution of the code to see what happens after a Checkbox.Value=False that ends up setting the Value back to True?

Do you need to use the MouseUp event or was that just one approach to workaround the problem?

Please clarify if I've misunderstood what you are wanting to do.
 
Last edited:
Upvote 0
Application.Caller differentiates between being called from VBA or from a worksheet formula.

Code:
Sub Button1_Click()
    MsgBox WhereCalledFrom()
End Sub

Function WhereCalledFrom()
    Select Case TypeName(Application.Caller)
        Case "Error"
            WhereCalledFrom = "I was called from VBA"
        Case "String"
            WhereCalledFrom = "I was called by a control"
        Case "Range"
            WhereCalledFrom = "I was called by a worksheet formula"
    End Select
End Function
 
Upvote 0
Application.Caller differentiates between being called from VBA or from a worksheet formula.

Code:
Sub Button1_Click()
    MsgBox WhereCalledFrom()
End Sub

Function WhereCalledFrom()
    Select Case TypeName(Application.Caller)
        Case "Error"
            WhereCalledFrom = "I was called from VBA"
        Case "String"
            WhereCalledFrom = "I was called by a control"
        Case "Range"
            WhereCalledFrom = "I was called by a worksheet formula"
    End Select
End Function

Interesting, when I execute this code, I provides "Error" when I would expect "String":

Code:
Private Sub myCheck_Click()

msgbox typeName(Application.Caller)

end sub

Thoughts?? This is an active x checkbox
 
Upvote 0
I can set the value, but I need to be able to manage the code execution. VBA is triggering the _click() code when I only need to set the appearance of the checkbox as clicked.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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