Here is my code:
For i = Z To lastRow
Range("$A" & i).Select
ActiveCell.FormulaRC1C = "='Sheet1'!$A" & i
etc, etc.
The last line is having a problem (obviously) because I can't seem to figure out the quotes needed. How do I format the double quotes? Is there a better way to say this?
I don't think the quotes are the problem (they look OK). You're using the FormulaR1C1 method, which expects the address to be in R1C1 notation (e.g. R3C4), whereas you're supplying the address in e.g. C4.
Try using .Formula
There's no readable way to insert quote marks into code that generates a string. You can try triple-quotes - """ - and good luck with your debugging.
I tend to use the chr() function, with the knowledge that character 34 is the double-quote:
strQ = "He said: " & chr(34) & "It's a dead parrot" & chr(34)
strQ = strQ & ", and explained: " & "It has ceased to be" & chr(34) & "."
Debug.Print strQ
Will concatenate this string:
He said: "It's a dead parrot", and explained: "It has ceased to be".
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
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'm trying to combine contents from multiple cells in excel. Here is an example of what i'm wanting to do:
(Cell1): John Doe
(Cell2): 5950
(Cell3): Autofill with "John Doe" XXXXXX5950
I know i can combine the cells using the formula =&[Cell1]&" XXXXXX"&[Cell2]
But I want it to put the " " on each side of the name, but the code won't except it since part of using the formula is using the " symbol. Any help on this?
Use two double-quotes inside a string to put a double-quote:
=""""&[Cell1]&""" XXXXXX"&[Cell2]
You can escape the " character in VBA by using double quotes.
Range("A1").Formula = """" & Range("B1").Value & """ XXXXXX" & Range("C1").Value
Or you can also use ASCII character 34 as well.
Range("A1").Formula = Chr(34) & Range("B1").Value & Chr(34) & " XXXXX" & Range("C2").Value
I have a paragraph which is included in a spreadsheet. I also have a cell reference within the paragraph. I can format the cell reference in the spreadsheet using the '&' operator, but I need to format it with VBA.
This is what I have inside of my subroutine:
Range("A53").Value = "'=Prices quoted are firm for '&Q3&' days from date of quotation for delivery within 6 months, unless specifically'"
I've tried several different variations of this with no luck. Does anyone know where I'm going wrong?
You'll want to use .Formula instead of .Value on the range:
Range("A53").Value = """=Prices quoted are firm for "" & Q3 & "" days from date of quotation for delivery within 6 months, unless specifically"""
Also, double quotes ("") when already inside quotes turn into a single quotes. That can get kind of nasty looking, so you could also use chr(34) like:
Range("A53").Value = chr(34) & "=Prices quoted are firm for " & chr(34) & " Q3 & " & chr(34) & " days from date of quotation for delivery within 6 months, unless specifically" & chr(34)
Which, in this case is probably even nastier looking so I'm not sure why I mentioned it.