Home » excel » Excel VBA Pass Arguments through Onaction with CheckBoxes

Excel VBA Pass Arguments through Onaction with CheckBoxes

Posted by: admin May 14, 2020 Leave a comment

Questions:

So i have been at this for a while now and I have searched through many websites and forums but alas I can not find a solution to my issue.

I am trying to add arguments to an .OnAction event for a Checkbox

So.. For example

Dim chk as Checkbox
    With chk
        .name = "chk" & .TopLeftCell.Offset(0, -7).Text
        .Caption = ""
        .Left = cel.Left + (cel.Width / 2 - chk.Width / 2) + 7
        .Top = cel.Top + (cel.Height / 2 - chk.Height / 2)
        .OnAction = "CheckboxHandle(chk)"
    End With

So if I was trying to call this sub -> Public Sub CheckboxHandle(obj As CheckBox)

It requries a CheckBox Object to be able to run (this can change to a shape/Object if necessary)

THINGS I HAVE TRIED

Changing the data type to object and shape however i couldn’t find a way to pass it through

Variations of the below statements

"""CheckboxHandle(chk)"""

"'CheckboxHandle" ""chk"" '"

Application.caller then looping through objects to find the object whit that name (this takes way too long as I have over 300 Checkboxes)

CONTEXT

In case the context helps I am trying to add a checkbox to every cell in a range and then have each one call the same method when they are clicked. I need the OnAction to send an Object as i look for the TopleftCell of the Object to change the colour of the adjacent cells

IN CASE IT IS HELPFUL
here is the method i would like to call from the OnAction Event

Public Sub CheckboxHandle(obj As CheckBox)
Dim rng As Range
'Sneaky sneaky changes
Application.ScreenUpdating = False


'For Loop to go through each of the cells to the left of the check box
For Each rng In Range(obj.TopLeftCell, obj.TopLeftCell.Offset(0, -7))
    With rng
        'if the checkbox is checked
        If obj.Value = -1 Then
            .Interior.Color = RGB(202, 226, 188)
            'Adds the date and the person so you know who did the edit
            obj.TopLeftCell.Offset(0, 1).Value = Now & " by " & Application.username
        Else
            'if it isn't checked
            .Interior.Pattern = xlNone
            'removes the edit name and date
            obj.TopLeftCell.Offset(0, 1).Value = ""
        End If
    End With
Next rng
'Shows all the changes at the same time
Application.ScreenUpdating = True

'Changes the value of the progress bar to represent the project completion
    If obj.Value = -1 Then
        ActiveSheet.Range("E1").Value = ActiveSheet.Range("E1").Value + 1 / 207
    Else
        ActiveSheet.Range("E1").Value = ActiveSheet.Range("E1").Value - 1 / 207
    End If
End Sub

Any help on this issue would be much appreciated

-Sebic0

How to&Answers:

I don’t think that you can pass an object via the OnAction. The OnAction-property is a string holding the name of a Sub (plus parameter).

You could try to pass the name of the checkBox instead. Note that you have to enclose the name of the checkbox in double quotes, so that you would get something like. CheckboxHandle "chk123":

 .OnAction = "'CheckboxHandle """ & .Name & """'"

And change your Action-routine

Public Sub CheckboxHandle(chbBoxName as string)
    dim chk as CheckBox
    Set chk = ActiveSheet.CheckBoxes(chkBoxName)
    (...)