Excel error: expected end of statement, what does this mean? - excel

I am trying to write this formula into a cell via my script:
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),"t","f")"
And it keeps coming up as an error, even though it works just fine in the actual sheet if I manually input it into the cell. What is it expecting me to do here?

You need to escape quotes. Try this:
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),""t"",""f"")"

The issue that you are running into is that " is interpreted as beginning or ending a VBA string. So VBA parses your expression as
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),"
with "garbage" at the end. This "garbage" is what it is complaining about. If you need to include a " within a VBA string, use "".

Related

Why does using [#Name] in Excel VBA string cause error 1004?

When I try to execute this statement using Excel VBA, it gives an error 1004:
Cells(C, 2).Value = "=IFERROR(XLOOKUP([#Name],Master_List_1[Name],Master_List_1[Email]),""error"")"
If I remove the brackets around #Name, it works fine. The other brackets in the string don't cause trouble. Somehow VBA is parsing the text string and objecting to having [#Name] in there.
If I put the statement in a cell by hand (with single quotes), it works as it should. (I did forget the = sign; that's been corrected.

Excel VBA FormatConditions drops invalid procedure call or argument on the client

We have an excel, which contains some VBA code. For a table we write some VBA code to set up some preparation like these:
Worksheets("Activity Features").range("ActivityFeaturesTable[ProtonProject]").Formula = "=VLOOKUP([Proton],Table_PT2_Projects,2,FALSE)"
Worksheets("Activity Features").range("ActivityFeaturesTable[SUM]").Formula = "=SUM(ActivityFeaturesTable[#[C0001]:[C0500]])*#IF([#Multiplier]="""",1,[Multiplier])"
With Worksheets("Activity Features").range("ActivityFeaturesTable[StartDate]")
.FormatConditions.Add Type:=xlExpression, Formula1:="=IF(INDIRECT(""RC[-1]"",0) = ""From StartDate to EndDate"", FALSE, TRUE)"
End With
This *.FormatConditions.Add Type:=xlExpression .. * line drops Invalid procedure call or argument error, however lines before this executes without any error.
The error occurs only on one client laptop, we cannot reproduce, several other users never experienced this error before. We checked the client computer settings, the Regional settings "List separator" was set to ";" instead of "," - but changing back to "," does not helped at all. And VBA code lines before this one also contains "," as separator in the formula.
We wonder why this error happens on the client side on this line?! Any idea? Is there any problem with the INDIRECT or RC in the formula?
Thanks in advance!

Setting Number Format to Accounting in VBA

With ActiveSheet
.Range("T26:T31").NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* " - "??_);_(#_)"
I am trying to set the format of a cell range to Accounting, I have looked at the number format (above) and tried setting it to that, but I get a Type Mismatch.
I also tried doing Debug.Print Application.ActiveCell.NumberFormatLocal to find out how excel reads it, copied that in and still with no luck.
Anyone got any ideas?
You need to double any quotation marks inside the format code, so:
.Range("T26:T31").NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* "" - ""??_);_(#_)"

Cannot use "from" in .numberformat vba

Hi I have 2 cells where user is entering values, placed next to each other like:
200 1200
Using .numberformat i want to change it to
From 200 To 1200
I can use any other word but "from" is giving me an Run-time error 1004
Code I wanted to use:
Range("B42:B44").NumberFormat = ("From" & "##0.0\°F")
Range("C42:C44").NumberFormat = ("To " & "##0.0\°F")
Any ideas how can I bypass this?
Test in a number format should be enclosed in quotes:
Range("B42:B44").NumberFormat = """From""##0.0""°F"""
or you can escape each character with \ as you did with the degree symbol, but that gets tedious!
This isn't a VBA problem. If you right click the cell and goto format/custom and type From ##0.0\°F you get an error. But regardless, you can fix it either manually or in VBA by escaping out the problem characters:
Range("B42:B44").NumberFormat = ("F\ro\m ##0.0°F")

Pass string into Range Function in VBA

I am trying to create a range in a function but the range varies so I am going to pass in the definition as a string but I am running into an error because it says it "Expected an array". So, I thought this was because the double quotes weren't included so I tried to include them in the string by doubling up on double quotes but now VBA is saying I have an invalid character in that string (that being the first dollar sign). I am really confused on how to fix this. Any help would be greatly appreciated.
gRange = ""$A$1:$F$2""
Whenever I have issues in VBA with extra quotes in strings, I always fall back on using Chr(34) to replace.
gRange = Chr(34) & "$A$1:$F$2" & Chr(34)
Chr() MSDN Reference
I found a solution using an if Statement like so:
If sheetName = "Totals" Then
ChartData.ActiveSheet.ListObjects("Table1").Resize Range("$A$1:$F$2")
Else
ChartData.ActiveSheet.ListObjects("Table1").Resize Range("$A$1:$G$2")
End If
Instead of using a string.

Resources