Inserting a formula containing an ampersand into a cell - string

I'm trying to create a formula in the A1 cell which contains an ampersand character that looks something like this in Excel:
="Hello, "&A2
My VBA code is as follows:
Worksheets("Sheet1").Range("A1").Formula = "=""Hello, "" & "&A2"
VBA is throwing errors, and I can't figure out why. I've tried using double ampersands, and double quotes in various places and can't get around this error.

Consider:
Sub dural()
Range("A1").Formula = "=" & Chr(34) & "Hello" & Chr(34) & "& A2"
End Sub

Please consider this pithy line:
[a1] = "=""Hello, "" & A2"
...which will evaluate to, "Hello, Dave"

You have one extra ":
Worksheets("Sheet1").Range("A1").Formula = "=""Hello, "" & A2"

Use the & character code of 38:
Worksheets("Sheet1").Range("A1").Formula = "=" & Chr(34) & "Hello, " & Chr(34) & Chr(38) & "A2"

This should work as well.
ActiveCell.FormulaR1C1 = "=""hello "" & A2"

Related

Inserting semicolon between two quotation marks in VBA

I am trying to insert a formula automatically within a certain range but somehow I am getting a runtime error 1004. Can someone please assist ?:
The Original in excel:
=#IF(ISNA(MATCH($B6;StepList;0));"";IF(#INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())="";"";INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())))
My Code:
Dim sMyString As String
sMyString = Chr(34) & Chr(34) & Chr(59) & Chr(34) & Chr(34)
.Range("C6:C25").Formula = "=#IF(ISNA(MATCH($B6,StepList,0)),"""",IF(#INDEX(StepIdTable,MATCH($B6,StepList,0),COLUMN())=" & sMyString & ",INDEX(StepIdTable,MATCH($B6,StepList,0),COLUMN())))"
The problem is "";"". I have tried everything to make this work
Does anyone have an idea ?
Not sure if this is the answer because I'm in a different locale and can't test it directly, but if all your other delimiters are commas, shouldn't the one in sMyString be as well?
sMyString = Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34)
where 44 is the code for a comma.
On the other hand, if they are all supposed to be semicolons, then the formula should have been
Range("C6:C25").Formula = "=#IF(ISNA(MATCH($B6;StepList;0));"""";IF(#INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())=" & sMyString & ";INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())))"
But when using Formula, you have to enter the formula string using commas as delimiters regardless of locale. However in your case, if you want to standardise on semicolons both in VBA and on your sheet, you could try FormulaLocal:
Range("C6:C25").FormulaLocal = "=#IF(ISNA(MATCH($B6;StepList;0));"""";IF(#INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())=" & sMyString & ";INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())))"
see this reference
BTW I find it helpful to put the whole formula string into a debug.print statement to see how it looks.
It's all about the number of double quotes. Try one of this approaches:
Sub test1()
sMyString = """"""";"""""""
frm = "=IF(F13=" & sMyString & ",1,0)" 'so
frm = "=IF(F13="""""";"""""",1,0)" 'or so
Debug.Print frm '=IF(F13=""";""",1,0) within the cell
Range("F13").Formula = frm
End Sub
Explanation: since the escape of double quotes is their doubling, each conversion of a string containing double quotes adds or subtracts (depending on the direction of the conversion) these pairs, resembling a cascading effect

Inserting Multiple Double Quotes In A Cell Using VBA

I have the following formula that I want to insert into a cell but it has multiple double quotes and I can't seem to make it work. There is plenty of solutions on the web but most have 1 or 2 sets of double quotes. I tried double quotes on all double quotes but it gives me a runtime error 438 object doesn't support this property
Here is the formula that I want to insert:
="Closing Costs" & " Current Buffer" & " " &TEXT('Closing Costs'!D32,"$ 0")
Here is the code that is not working:
ActiveSheet.Range("F18").Value = "=""Closing Costs"" & ""
Current Buffer"" & "" "" &TEXT('Closing Costs'!D32,""$ 0"")"
The following should work (all on one line):
ActiveSheet.Range("F18").Value = "=""Closing Costs"" & "" Current Buffer"" & "" "" &TEXT('Closing Costs'!D32,""$ 0"")"
It looks the same as your sample - but perhaps looks here are deceptive.
Using the chr() command can be helpful in these scenarios. chr(34) returns a double quote character, so the below should do the trick.
ActiveSheet.Range("F18").Value = "=" & chr(34) & "Closing Costs Current Buffer" & chr(34) & " " & chr(34) & TEXT('Closing Costs'!D32," & chr(34) & "$ 0" & chr(34) & ")"
It’s a little bit longer, but I find using chr(34) to be a little easy to determine quotation placement.

How to apply quotes in formula with multiple criteria?

I need to input a formula into a macro but I get
Application-defined or object-defined error
The formula is
=SUMIFS(FBL5N!O:O,FBL5N!D:D,Y1120_CREDIT!B6,FBL5N!S:S,{"<>0","<>3","<>9","<>O","<>D"})
I have this in the code
Range("P6").Formula = "=SUMIFS(FBL5N!O:O,FBL5N!D:D,Y1120_CREDIT!B6,FBL5N!S:S,{" & "<>0" & "," & "<>3" & "," & "<>9" & "," & "<>O" & "," & "<>D" & "})"
I have tried putting quotations in different ways and still with the same result.
Just double all your quotes in the string:
Range("P6").Formula = "=SUMIFS(FBL5N!O:O,FBL5N!D:D,Y1120_CREDIT!B6,FBL5N!S:S,{""<>0"",""<>3"",""<>9"",""<>O"",""<>D""})"

Formula is giving error when I add "text"

I'm trying to write a Macro that will do the formatting of a report. All is well up until I try to create the transaction reference number.
The formula basically goes =A2 & "-" & B2 & "ABCD"
When I insert this into my macro it becomes
Range("C2").Formula = "=A2 & "-" & B2 & "ABCD""
As soon as I add my hyphen or the letters at the end it gives me "Compile Error: Expected: end of statement"
What am I doing wrong?
Edit: So the output would be contentOfA2-contentOfB2 ABCD
Try this. When adding quotes inside a string, double up the quotes so the program knows you aren't simply ending the string.
Range("c2").Formula = "=A2 & ""-"" & B2 & ""ABCD"""
Example..
Range("A1").value = "adding a quote inside a string ""so and so said something"" is like this."
output: adding a quote inside a string "so and so said something" is like this.
Try to do it reversed - e.g. write the formula in excel like this:
Then select the formula and write the following in the VBA:
Public Sub TestMe()
Debug.Print Selection.Formula
End Sub
On the direct window you would have this:
=A2&"-"&B2&"abcd"
This input should be edited a bit, because it comes from the console and you need to escape the ". Like this:
"=A2 & ""-"" & B2 & ""abcd"""
You can elaborate further, by writing Debug.Print Selection.FormulaR1C1, if you need it.
At the end, you can use something like this, to escape the counting of the parenthesis. It would work ok:
Public Sub PrintMeUsefulFormula()
Dim strFormula As String
Dim strParenth As String
strParenth = """"
strFormula = Selection.Formula
strFormula = Replace(strFormula, """", """""")
strFormula = strParenth & strFormula & strParenth
Debug.Print strFormula
End Sub
It changes the single quote to a double and then it puts the formula in a quote. In my code, it is code strParenth.

Stuck. I need some VBA code to insert double-quotes around strings in each cell in an excel range

I'm familiar with VB.NET, but VBA in excel has me stumped.
With my best attempt, I get a "Type mismatch" error:
Sub AddQuotes()
For Each x In Range("List").Cells
x.Text = "*" * " & x.text & " & "*"
Next
End Sub
Try using chr(34) for the double-quote character
eg chr(34) & x.text & chr(34)

Resources