Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Arguments to Method registered with onAction Event(VBA - Excel)

Tags:

excel

vba

How do you pass an argument to a method which is registered with the onAction event in Excel VBA?

The code I have is:

With ActiveSheet.CheckBoxes.Add(rCell.Left, rCell.Top, rCell.Width, rCell.Height)
        .Interior.ColorIndex = xlNone
        .Caption = ""
        .OnAction = "CheckboxChange"
End With

I want to pass the "rCell" to the "CheckboxChange" sub routine. Any way to do that. Basically I want to know the cell in which the checkbox was present in the CheckboxChange sub routine.

like image 216
Manoj Avatar asked Jan 24 '23 05:01

Manoj


2 Answers

Change this:

.OnAction = "CheckboxChange"

To this:

.OnAction = "'CheckboxChange""" & rCell & """'"

""" = 3 double quotes

"""'" = 3 double quotes & 1 single quote & 1 double quote

like image 104
guitarthrower Avatar answered Jan 25 '23 19:01

guitarthrower


with chr(34) instead of quote ("), it might be easier,

I have an example with 3 string arguments:

.OnAction =  "'" & ThisWorkbook.Name & "'!'Importer_Items_Temp_par_Menu_Déroulant " & Chr(34) & Nom_Fichier & Chr(34) & " , " & Chr(34) & Old_Val & Chr(34) & " , " & Chr(34) & ThisWorkbook.Sheets("Import_Objets").Cells(Item_Num, q * 2).Value & Chr(34) & "'"

.

Where Importer_Items_Temp_par_Menu_Déroulant is a macro,

Nom_Fichier is a string variable,

Old_Val too,

ThisWorkbook.Sheets("Import_Objets").Cells(Item_Num, q * 2).Value is a string too, from a cell in a sheet.

like image 26
Patrick Lepelletier Avatar answered Jan 25 '23 20:01

Patrick Lepelletier