Excel, VBA: How to pass multiple variables to .OnAction - excel

I am trying to pass multiple variables to a .OnAction call for a button. I have not been able to find answers that relate to multiple variables, and I can do it with just one variable. Here is what I have that works for one variable:
shpBar.OnAction = "'Button_Click """ & strText & """'"
How can I add two other variables to this (such as VarA and VarB)?

Assuming that VarA and VarB are variants or numeric variables, this would work:
.OnAction = "'Button_Click """ & strText & """," & varA & "," & varB & " '"
If they are strings, you need to add two more double-quotes around each variable name.
.OnAction = "'Button_Click """ & strText & """,""" & varA & """,""" & varB & """ '"

Related

What format do I use for a string variable in a query

I am performing a web query and want to filter a column by a cell reference.
ThisWeek = ThisWorkbook.Sheets("Summary").Range("A1").Value
LastWeek = ThisWorkbook.Sheets("Summary").Range("M1").Value
ActiveWorkbook.Queries.Add Name:="1245", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source =
Json.Document(Web.Contents(""" & finalstr & """))," & Chr(13) & "" &
Chr(10) & " each ([Column1.report_begin_date] = " < " & ThisWeek & " > "
or [Column1.report_begin_date] = " < " & Last Week & " > ")"
#""Filtered Rows"""
ThisWeek and LastWeek are both strings that change each day and I want the query to filter out by these dates. VBA doesn't recognize the way Im formatting my variable names and doesn't recognize them as an editable variable.
Basically what you want to do is add quotation marks for your date string variable, correct?
You can achieve that by using: """" (4 times quotation marks)
Which will create a simple "
So if you add it to both sides of your string variable, you will get what you need, as follows:
[...] " < " & """" & ThisWeek & """" & [...]
Hope this helps

Escaping for Countif macro with wildcard cell reference

I cant for the life of me get the escapes working correctly for my macro. I am trying to input a Countif function that uses wildcards on either side of a cell reference value, it also finds the last row within a separate tab to help define the range to count in.
I want the target cell to contain the following string:
=COUNTIF('NCR''s'!$B$8:$C$117,"*"&'Design Baseline'!B8&"*")
The following is the code that attempts to do this:
Sub test()
Dim Wild1 As String
Dim Wild2 As String
Wild1 = Chr(34) & Chr(42) & Chr(34) & Chr(38)
Wild2 = Chr(38) & Chr(34) & Chr(42) & Chr(34)
'code for counting instances of parts on each tab
Sheets("NCR's").Select
Cells.Select
Selection.EntireRow.Hidden = False
NCRlastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Sheets("Design Baseline").Select
Cells.Select
Selection.EntireRow.Hidden = False
'Range("AB8").Formula = "=COUNTIF('NCR''s'!$B$8:$C$" & NCRlastRow & "," & Chr(34) & "*" & Chr(34) & "&" & "'Design Baseline'!B29" & "&" & Chr(34) & "*" & Chr(34) & ")"
Range("AB8").Formula = "=COUNTIF('NCR''s'!$B$8:$C$" & NCRlastRow & "," & Wild1 & "'Design Baseline'!B29" & Wild2 & ")"
End Sub
As you can see in the line beginning with ', I have tried to use the Chr(ref) method to get around this as well as using user defined strings for the ", * and & symbols. I have also attempted the double " escape and even ". Not sure how to get around this o.O
The following line was what I used before realizing I needed wildcards and worked fine, not sure if it would help but I'll put it here regardless:
Range("AC8").Formula = "=COUNTIF('NCR''s'!$B$8:$B$" & NCRlastRow & ",'Design Baseline'!B8)"
Any help would be greatly appreciated!
You need to escape the double quotes in your formula.
Something like this:
Dim formula As String
formula = "=COUNTIF('NCR''s'!$A$8:$C$" & NCRlastRow & ",""*"" &'Design Baseline'!B8&""*"")"
Debug.Print formula
Range("AB8").formula = formula

Excel Hyperlink does not work

I have been batling this suposedly simple problem for 2 hours now and I just cant figure it out. I am trying to make a link and no matter how many " marks I try it always ends up in error or the link says & Poracun(Z).Ime & Can anyone find me a way that I can change the link name ?
Here is my code:
Cells(i, "B").Formula = "=HYPERLINK(""[" & ActiveWorkbook.Path & "\" & "DN_Pokalkulacija.xlsx" & "]" & "'" & "Sheet1" & "'" & "!E" & Poracun(Z).Vrstica & ""","" & Poracun(Z).Ime & "")"
Two issues:
Your " delimiting is not quite right
Formula expects ' around the file name, not the sheet. Like '[FileName]SheetName'!
Try
Cells(5, "B").Formula = "=HYPERLINK('[" & ActiveWorkbook.Path & "\" & "DN_Pokalkulacija.xlsx" & "]" & "Sheet1" & "'" & "!E" & Poracun(Z).Vrstica & ",""" & Poracun(Z).Ime & """)"
I have figured it out , it was quotation problem, i forgot all about it and now i have read up on it inside quotation string you need to make double quotation to use as single in final string. Here is a working link :
g = "=HYPERLINK(""[" & ActiveWorkbook.Path & "\" & "DN_Pokalkulacija.xlsx" & "]" & "'" & "Sheet1" & "'" & "!E" & Poracun(Z).Vrstica & """,""" & Poracun(Z).Ime & """)"
It was all in quotation (") marks :)

Excel vba formula.local error

IF(ISBLANK(" & variable & "!G2);" & """""" & ";IF(ISERROR(DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2)));" & """""" & "; (DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2))))"
variable is a name of variable which contains name of sheet.
I get: "application-defined or object-defined error.
Can someone help me?
Try to use this one (removed ( before DATE):
Range("A1").FormulaLocal = "=IF(ISBLANK(" & variable & "!G2);" & """""" & ";IF(ISERROR(DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2)));" & """""" & "; DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2))))"
The main problem is that you are using semi-colons instead of commas in the if statement.
Initially I thought you were trying to do this in the worksheet, in which case you would have needed to use INDIRECT().
However on re-reading your question I realised you were trying to build a formula string in VBA.
The formula string you are building is not the correct syntax. For example you use ";" instead of ",". I also think you have the wrong number of brackets.
If I were you I would debug this by doing something like:
sFormula = "IF(ISBLANK(" & variable & "!G2);" & """""" & ";IF(ISERROR(DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2)));" & """""" & "; (DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2))))"
Debug.Print sFormula
And then pasting the resulting string into the spreadsheet and see if it works, and edit as appropriate.

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

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.
Change this:
.OnAction = "CheckboxChange"
To this:
.OnAction = "'CheckboxChange""" & rCell & """'"
""" = 3 double quotes
"""'" = 3 double quotes & 1 single quote & 1 double quote
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.
The Excel.Checkbox control has the property LinkedCell. Set this property to the value of rCell.Address or the like. Set OnAction to the CheckboxChange macro. To get the clicked checkbox, use Evaluate(Application.Caller) inside of CheckboxChange. Application.Caller will be the checkbox's name, and Evaluate returns the checkbox object itself. Using this object you can get its Name or LinkedCell.

Resources