Inserting semicolon between two quotation marks in VBA - excel

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

Related

Writing Ranges into formulas with VBA

I am using variable ranges, and want to insert those ranges into a formula that can be saved in a document that does not use VBA.
Do I have to use a with statement?
My If Statement works until I try and insert a relative range. I searched and didn't see much on this, so this issue is probably so simply I am an idiot.
The program will be doing things all over the worksheet, so relative coding is needed. The number of items in the range changes with each iteration. I will be using an end range integer [myRange] for the number instead of 10, but I wanted to simplify what I am struggling with into the smallest steps.
Dim Test As String
Test = "=IF(COUNTIF(" & ActiveCell.Offset(1, 0).Address & ":" & ActiveCell.Offset(10, 0).Address & "" _
& ")," & """Not Complete""" & "," & "" & """"")"
ActiveCell.Formula = Test
Desired output in target cell:
"=IF(COUNTIF($J$3:$J$12),"Not Complete","")"
As mentioned in comments, your COUNTIF statement is currently invalid. What are you counting in the range J3:J12? This needs to be added (in place of "CRITERIA" in below solution) before your equation will be valid
Test = "=IF(COUNTIF(" _
& ActiveCell.Offset(1).Resize(10).Address(True, True) _
& ",""CRITERIA""), ""Not Complete"", """")"
If you want your output to actually have the quotes you can output
Chr(34) & Test & Chr(34) which seems cleaner than wrapping the whole string in quotes from a readability point of view IMO

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 insert variable value into getpivotdata formula using excel vba macro

So I've learned pivot tables are tricky, so are string variable syntax, and so is the use of quotations in VBA. I'm trying (and failing) to use all three!
I am trying to collect one piece of data from a very large/complex pivot table for a large number of job numbers. I want to select a job number in A1, and have the pivot table formula automatically update with the selected job number to return a number result.
I am currently having two problems: 1) the syntax to get the job number in quotations for the 'getpivotdata' formula isn't working and 2)it is dropping off the zeros in the job number when it executes the code.
My code is listed below, when I execute it I am hoping to see this formula populate in A2:
=GETPIVOTDATA("Part Number",' Parts Status '!$A$8,"CHAR_FIELD3","11-008","MATERIAL STATUS MASTER","Avail")
but instead get:
=GETPIVOTDATA("Part Number",' Parts Status '!$A$8,"CHAR_FIELD3",11-8,"MATERIAL STATUS MASTER","Avail")
My code is here
Sub Macro1()
Dim jobnumber As String
jobnumber = Worksheets("Macros test").Cells(1, "A").Value
Sheets("Macros test").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"=GETPIVOTDATA(""Part Number"",' Parts Status '!R8C1,""CHAR_FIELD3""," & jobnumber & ",""MATERIAL STATUS MASTER"",""Avail"")"
If you need the quotes there, change it to this:
"=GETPIVOTDATA(""Part Number"",' Parts Status '!R8C1,""CHAR_FIELD3"",""" & jobnumber & """,""MATERIAL STATUS MASTER"",""Avail"")"
Also, you can add a temporary msgbox after setting jobnumber to make sure it is what you think it is:
MsgBox(jobnumber)
I've always used the Chr() function to deal with quotes in VB. Double quote is Chr(34) and single quote is Chr(39). The problem you experienced was that while jobnumber is defined as a string you didn't have the double quotes around it. The result is Excel removed the leading zeros on the formula 11-008, which Excel saw as Eleven minus eight. Adding the double quotes (Chr(34)) around jobnumber solved the problem.
I always break these long strings into smaller pieces so that I can see what I've typed. The code below uses a little private function to build your pivot string.
Public Sub PivotTest()
Dim jobnumber As String
jobnumber = Worksheets("Macros test").Cells(1, "A").Value
Sheets("Macros test").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = BuildPivotString(jobnumber)
End Sub
Private Function BuildPivotString(ByRef jobNum As Variant) As String
Dim retVal As String
retVal = "=GETPIVOTDATA("
retVal = retVal & Chr(34) & "Part Number" & Chr(34)
retVal = retVal & ",' Parts Status '!R8C1,"
retVal = retVal & Chr(34) & "CHAR_FIELD3" & Chr(34) & ","
retVal = retVal & Chr(34) & jobNum & Chr(34) & ","
retVal = retVal & Chr(34) & "MATERIAL STATUS MASTER" & Chr(34) & ","
retVal = retVal & Chr(34) & "Avail" & Chr(34) & ")"
BuildPivotString = retVal
End Function

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