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.
Related
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
I'm experiencing the following issue:
Sheets("Workbook 1 ").Range("N" & integerv).Formula = "=Workbook2!""" & column_string & """ 3)"
(integerv being some integer) keeps returning " Object-defined or application-defined error".
column_string is a string variable obtained by
column_string = Split(Cells(a,b).Address(True,False), "$")(0)
essentially just containing the column letter of the cell I want to reference.
Am I passing the string variable incorrectly ? And what would my code have to look like if I wanted to additionally pass an integer variable instead of "3" ?
Any help appreciated, I've been stuck with this for a fair while now.
Just a Thought or Two About Formulas in VBA
How would you write your formula for column A in Excel?
=Workbook2!A3
In VBA you would do this:
rng.formula = "=Workbook2!A3"
You can aslo write it like this:
rng.Formula = "=Work" & "book2" & "!A3"
or like this:
rng.Formula = "=" & "Work" & "book" & "2" & "!" & "A3"
But of course in this case (your post) you wanna write it like this:
rng.Formula = "=Workbook2!" & "A" & "3"
Now when you make it dynamic, you know what to do:
rng.Formula = "=Workbook2!" & column_string & "3"
As FunThomas mentioned in the comments you can use a variable (and you should especially when there is a more complicated one). Then you could create a new Sub just for the formula and write...
Dim strFormula As String
strFormula = "=Workbook2!" & "A" & "3"
...and in the Immediate window (CTRL+G) see the written result with...
Debug.Print strFormula
...and modify the formula until you get it right.
You my try the formula in this way:
Dim b As Long, column_string As String
b = 2: column_string = "C" 'the result simulation of your extraction using Split
Sheets("Workbook 1 ").Range("N" & integerv).Formula = "=Workbook2!" & column_string & 3
'having the b variable value, no need to extract the column letter:
Sheets("Workbook 1 ").Range("N" & integerv).Formula = "=Workbook2!" & cells(3, b).Address(0, 0)
this line is giving me an error,please help me to troubleshoot
ActiveCell.Formula = "=STDEVA(range(" & """" & area & """" & ")"
here area is a string variable which is having the value it supposed to have just fine
So the main issue is syntax and more specifically the quotation marks I guess,any help is appreciated,even though I can use stdev I still like to use stdeva in my macro although the difference is very subtle.
This assumes that area is a String`:
Sub gahgsd()
Dim area As String
area = "A1:A4"
ActiveCell.Formula = "=STDEVA(" & Range(area).Address & ")"
End Sub
or:
Sub gahgsd()
Dim area As String
area = "A1:A4"
ActiveCell.Formula = "=STDEVA(" & area & ")"
End Sub
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
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"