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
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 cannot figure this out - I either get a Named? error or Application defined error on trying everything.
Dim sAmt As Integer
Dim sOriginalAmount As Integer
sOriginalAmount = ActiveCell.Value
sAmt = Application.InputBox("How much additional cost?", Type:=2)
ActiveCell.Formula = sOriginalAmount + sAmt
What I want Excel to do is write the formula in the cell not the output.
For example - Currently if I have sOriginalAmount as 15 and I enter sAmt as 20, on running the code, it gives the answer 35 in the cell, but I want it to write =sum(valueofsOriginalAmount + valueofsAmt) so that at a later time I can see what was the original amount and how much was added.
Can anyone please help? I have tried & "" in various combinations but I just can't get it. Apologies for such a noobie question, but I am self learned and I code for my work only.
This should work.
ActiveCell.Formula = "=" & sOriginalAmount & "+" & sAmt
Or if you actually want to use SUM.
ActiveCell.Formula = "=SUM(" & sOriginalAmount & "," & sAmt & ")"
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 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.
Dim dflrow As Integer
ActiveWorkbook.Sheets("ElecTM").Range("I1048576").Select
dflrow = Selection.End(xlUp).Row
' ActiveWorkbook.Sheets("ElecTM").Range("AG" & dflrow & ":" & "AJ" & dflrow).Select
ActiveWorkbook.Sheets("ElecTM").Range("AG6").Formula = "=CONCATENATE(Year(I6),"w",WeekNum(I6))"
Selection.FillDown
Here I am getting an error saying syntax error on fifth line. I have tried'.value' instead of '.Formula'. But that didn't help.
You are missing double quotes in "w"
ActiveWorkbook.Sheets("ElecTM").Range("AG6").Formula = _
"=CONCATENATE(Year(I6),""w"",WeekNum(I6))"
You have to pad the " with extra " if using between ""
EDIT: When in doubt, record a macro ;)
w should be in double quotation
ActiveWorkbook.Sheets("ElecTM").Range("AG6").Formula = "=CONCATENATE(Year(I6),""w"",WeekNum(I6))"