I've searched extensively for the answer to my question and I'm at an impasse.
I'm trying to pass a formula to excel with applescript. The problem is the quotes around the spaces that I want to place between fields. I've used "\" to escape the quotes but it throws an error. To make it more complicated the row number is a variable "i". Here is the formula in excel format:
=CONCATENATE(A2," ",B2," ",C2," ",D2)
Here is the formula in applescript-ese (works but does not produce spaces in data):
set rowCount to (((count of rows of used range of active sheet)) + 1)
repeat with i from rowCount to 2 by -1
set formula of row i of column 15 to "=CONCATENATE(A" & i & "," & ",B" & i & "," & ",C" & i & "," & ",D" & i & ")"
Here is the formula with escaped quotes to add the spaces (gives error):
set formula of row i of column 15 to"=CONCATENATE(A" & i & ","\" \" & ",B" & i & ","\" \" & ",C" & i & ","\" \" & ",D" & i & ")"
I get a sytax error on the first slash; Expected end of line etc. but found unknown token. I have a feeling I'm either missing a few sets of double quotes or I'm making this way more complicated than it needs to be. Any ideas? Should I be going about this differently?
Thanks in advance!
EDIT
Taking another look at it I think you have too many quotes when you use the \ character to escape the double quote. Instead of ","\" \" & ",B" try ",\" \",B" so your line would look like this:
set formula of row i of column 15 to "=CONCATENATE(A" & i & ",\" \",B" & i & ",\" \",C" & i & ",\" \",D" & i & ")"
In excel VBA you can double quote a double quote to get it to show up in a string like this:
"=CONCATENATE(A" & i & "," & """ """ & ",B" & i & "," & """ """ & ",C" & i & "," & """ """ & ",D" & i & ")"
Or you can try using ascii character for space which is Chr(32)
Related
Im currently figuring out how to write "&" in a formula so that VBA recognizes it as a string and not as the &-operator.
My formula looks like this:
"=AVERAGEIF(RC[-4]:R[" & Total & "]C[-4],"">""&0.5*MAX(RC[-4]:R[" & Total & "]C[-4]))"
But it should look something like this, as I want to use the variable z instead of 0.5
"=AVERAGEIF(RC[-4]:R[" & Total & "]C[-4],"">"" & Chr(34)& Chr(38) & Chr(34)& z & *MAX(RC[-4]:R[" & Total & "]C[-4]))"
As you see I already tried to write the "" and the & as ASCII but it seems that even this doenst work.
Thanks in advance for your help!
EDIT: The result should look like this:
=AVERAGEIF(C31:C6413;">"&0.5*MAX(C31:C6413))
EDIT: will post another question with the whole code. See here
I wouldn't use Chr(34) for " and Chr(38) for &, they're unnecessary indirection making the code harder to reason about. & doesn't need escaping, and to have a " literal inside a string you escape it by doubling it up, so """" is a string containing a single " character.
Concatenating long strings with escaped double-quotes can be a frustrating experience... what if we broke down the steps, and added a bit of space around operators?
The cognitive load is immediately reduced, and any error in the logic is much easier to spot:
Dim avgSourceRangePart As String
avgSourceRangePart = "RC[-4]:R[" & Total & "]C[-4]"
Dim maxPart As String
maxPart = "MAX(RC[-4]:R[" & Total & "]C[-4])"
Dim avgConditionPart As String
avgConditionPart = """>"" & " & z & " * " & maxPart & ")"
Now the final concatenation looks like this:
"=AVERAGEIF(" & avgSourceRangePart & "; " & avgConditionPart ")"
This should be what you're looking for:
"=AVERAGEIF(RC[-4]:R[" & Total & "]C[-4],"">"" &" & z & "*MAX(RC[-4]:R[" & Total & "]C[-4]))"
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.
I need to input a formula into a macro but I get
Application-defined or object-defined error
The formula is
=SUMIFS(FBL5N!O:O,FBL5N!D:D,Y1120_CREDIT!B6,FBL5N!S:S,{"<>0","<>3","<>9","<>O","<>D"})
I have this in the code
Range("P6").Formula = "=SUMIFS(FBL5N!O:O,FBL5N!D:D,Y1120_CREDIT!B6,FBL5N!S:S,{" & "<>0" & "," & "<>3" & "," & "<>9" & "," & "<>O" & "," & "<>D" & "})"
I have tried putting quotations in different ways and still with the same result.
Just double all your quotes in the string:
Range("P6").Formula = "=SUMIFS(FBL5N!O:O,FBL5N!D:D,Y1120_CREDIT!B6,FBL5N!S:S,{""<>0"",""<>3"",""<>9"",""<>O"",""<>D""})"
I am attempting to sum up the numbers in a certain row by adding a formula in vba but It doesn't seem to like this Formula.
Each Cell Looks like this [ Vertical Lines are not included in cells ]:
30 (Gold) | 25 (Silver) | 20 (Gold) | 13 (Green)
So it needs to remove everything after "("
Worksheets("Season 2014-2015").Cells(lastSeasonRow, 14).Formula = "=SUM(LEFT(B" & lastSeasonRow & ":L" & lastSeasonRow & ",InStr(1, B" & lastSeasonRow & ":L" & lastSeasonRow & ","" (""))"
Try an array formula involving the SUMPRODUCT function using the IFERROR function to discard non-valid/blank entries,
With Worksheets("Season 2014-2015").Cells(lastSeasonRow, 14)
.FormulaArray = "=SUMPRODUCT(IFERROR(VALUE(LEFT(TRIM(B" & lastSeasonRow & ":L" & lastSeasonRow & "), FIND(CHAR(32), TRIM(B" & lastSeasonRow & ":L" & lastSeasonRow & "&CHAR(32))))), 0))"
End With
I used CHAR(32) (CHAR function) because I avoid dealing with quotes in quoted strings. The TRIM function can be left out if you do not have any leading spaces in the actual data.
I am creating a formula to count based upon 2 conditions. My logic is wrong - again.
When I (manually) enter enter code the summation formula (COUNTIFS) into correct cell itself, it sums correctly:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
When I execute the following code line, I do not get any errors, but instead, all the sums are zero.
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>4" & Chr(34) & ")"
I realized that the COUNTIFS was comparing the value in column S to the string "<>4". And none of the cells contain that string. This is why all my values were zero. I do not want the comparison to be against that string. I want the comparison to be column S value NOT equal to 4.
So, I changed the line to (and variations to get it to work):
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>" & Chr(34) & "4)"
So, I am at a loss. (1) I manually enter the formula and it works. (2) I build it in VBA, and it does not sum correctly. (3) I look up how to do build the formula correctly, and I get errors time and time again. It seems like the only way to get this to work is to keep the quotes within quotes, but I don't want to compare against the string.
Does this make sense? I'm not liking my worksheet anymore. It is no longer any fun at all. :(
Thank you so much.
I think all the Chr(34) are getting in the way of you viewing your formula correctly. try using "" to make the quote for the formula.
.Formula = "=COUNTIFS(E4:E1362,""Requirement"",S4:S1362,""<>4"")"
with your Start and Finish functions, that would change the formula to:
.Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & _
",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
Well, One of the problem I found there was quite simple, instead using "," you must use ";".
It depends on the version you used, some using "," and some others using ";". you can just try it, I hope it solved your problem.
and second one "Requirement" there, I think you should use double quote instead.
and for your case To meet This condition:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
Use This:
"=COUNTIF(E" & Start(groups) & ":" & "E" & Finish(groups) & "," & """Requirement""" & "," & "S" & Start(groups) & ":" & "S" & Finish(groups) & "," & """<>4"")"
I think it Should Work (if I don't miss any Quotes thought).
... Try removing the chr(34) and the extra quotations - It should look exactly like the "Requirement" criteria:
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & ",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
After running a quick test with your initial code
Range("G1").Formula = "=COUNTIFS(E1" & ":E20" & "," & "Requirement" & ",S1" & ":S20" & "," & Chr(34) & "<>4" & Chr(34) & ")"
Modified slightly to account for your groups variable cell G1 ends up with the following formula:
=COUNTIFS(E1:E20,requirement,S1:S20,"<>4")
which when tested works absolutely fine for your second if statement S1:S20,"<>4".
I would guess that the problem lies with your first statement. What is in your requirement range and what data are you trying to match with.
I set up the requirement named range to be cell D5, I entered a 5 in there and the formula would increment each time I added an extra 5 to the range E1:E20.
I then started typing 4's into the range S1:s20 and this decreased my count.
edit
The last thing that I can suggest is to add in a helper column in column F that evaluates row to see if it matches the requirement condition, then it would be a simple matter of making your formula:
=COUNTIFS(F1:F20,TRUE,S1:S20,"<>4")