Formula works in excel but not in vba - vba

the below formula works in a cell, but when I try to use it in VBA it gives syntax error. Why is it and what is the solution?. Thanks.
ThisWorkbook.Sheets("Sheet2").Cells(Lastrow + 1, 9).Formula = "=(SUMIFS(Sheet1!$B:$B,Sheet1!$O:$O,">0")/SUM(Sheet1!$B1:$B1000))*100"

The problem is with ">0" to use quotations inside a String you need to do the following:
"">0"" with double quotations VBA understands its a String inside the String.
instead of the end " of the String >0 some code and " the start of a new String.

You need to use single quotes for the string otherwise the string ends and will throw an error but there may be something else going on. What is your exact error?

Related

How to fix this error 1004? Rewrite the line

I have the following line of code in VBA:
Range("A40:A64").Formula = "=IF(index(optional_processes,row()-39)=0;"";optional_processes,row()-39))"
Without the if-statement, some cells display 0. I want these 0to be blanks. That's what the if-statement should do. I think it gives the application or object-defined error because of the double quotation marks in the statement. How can I this get blank cells instead of 0?
Even if you local Excel uses ; (semicolon) as parameter separator in formulas, when writing a formula with VBA, you have to use , (comma)
Beside this, there are some more issues with the formula:
You are missing a index at the else-part
Use """" rather than "" like Kresimir suggests
Use an ISERROR like Kiran writes in his first suggestion
I think this will work:
Range("A40:A64").Formula = "=IF(isError(index(optional_processes,row()-39)),"""",index(optional_processes,row()-39))"
Try this:
Range("A40:A64").Formula = "=IF(ISERROR(index(optional_processes,row()-39)),'',index(optional_processes,row()-39))"
OR
Range("A40:A64").Formula = "=IF(index(optional_processes,row()-39)=0,"""",index(optional_processes,row()-39))"

Excel VBA Formula Inside a String

Trying to put a formula inside a string and can't get it working. Can someone help me turn this into a string? I know I need to do ""&"" in places, I just don't know where....
Dim form As String
form = =IFERROR(GETPIVOTDATA("[Measures].[Sum of Amount]",Pivot!$A$3,"[qAllData].[Year]","[qAllData].[Year].&[2.016E3]","[qAllData].[Month Name]","[qAllData].[Month Name].&["&$B9&"]","[qAllData].[PRODUCT_CODE]","[qAllData].[PRODUCT_CODE].&["&C$3&"]","[qAllData].[Product Name]","[qAllData].[Product Name].&["&C$4&"]")-C8-C7-C6-C5,0)
If your formual is verbatim what you have in the spreadsheet but you want to insert it with VBA then just:
Dim form As String
form = "=IFERROR(GETPIVOTDATA(""[Measures].[Sum of Amount]"",Pivot!$A$3,""[qAllData].[Year]"",""[qAllData].[Year].&[2.016E3]"",""[qAllData].[Month Name]"",""[qAllData].[Month Name].&[""&$B9&""]"",""[qAllData].[PRODUCT_CODE]"",""[qAllData].[PRODUCT_CODE].&[""&C$3&""]"",""[qAllData].[Product Name]"",""[qAllData].[Product Name].&[""&C$4&""]"")-C8-C7-C6-C5,0)"
This simply replaced each " by "" and wrapped the whole thing in quotes. VBA accepts it as a valid formula, though I am not sure that this is what you really want.
If it is a working formula and you just want it in a variable as a String, all you need to do is start and end it with double quotes (") and then double all inner quotes (""), like so:
form = "=IFERROR(GETPIVOTDATA(""[Measures].[Sum of Amount]"",Pivot!$A$3,""[qAllData].[Year]"",""[qAllData].[Year].&[2.016E3]"",""[qAllData].[Month Name]"",""[qAllData].[Month Name].&[""&$B9&""]"",""[qAllData].[PRODUCT_CODE]"",""[qAllData].[PRODUCT_CODE].&[""&C$3&""]"",""[qAllData].[Product Name]"",""[qAllData].[Product Name].&[""&C$4&""]"")-C8-C7-C6-C5,0)"

How to count the number of empty spaces in front of a String in VBA?

I have a YAML file which I am reading using VBA Excel plugin. I am getting a String of each line. I need to count the number of empty spaces in the front of the first line so that it will be a marker to find the next key. Let me know how to do that.
Option Explicit
Function empty_spaces(str_input)
empty_spaces = Len(str_input) - Len(LTrim(str_input))
End Function
harun24hr, your answer does not work with " My String " and similar.

Isolate a a substring within quotes from an entire line

To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))

Expected end of Statement Error on Simple Formula Insert

###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"})"
I want VBA to do this simple formula but getting "Expected: end of Statement" error.
It seems that I need to define something as VBA doesn't recognize character "{}" the brackets.
Assuming that ### actually symbolizes a cell object (otherwise you would get a compile error):
###.Value = "=LOOKUP(LEFT(W2),{""C"",""A"",""B""},{""Pick Up"",""Collect"",""Prepaid""})"
Also, I thought that you would have to change .Value to .Formula, but I tested and both ways work.
It might be requiring you to end the script like this
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"});"
OR
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"})";
NOTICE: the Semi-colon at the end ';'.
I'm not a VBA user for a long time. but just try. Delete this answer if its not good enough.