VBA OnTime with string argument containing apostrophe - excel

I have a problem with the following OnTime line:
Application.OnTime printTaskTimers(UBound(printTaskTimers)), _
"'PrintTask """ & cRng.Value & """, " & cRng.Offset(0, 1).Value & " '"
cRng.Value is worksheet name and cRng.Offset(0, 1).Value is the number of pages. It works correctly for all normal sheet names, apart from 2 which contain apostrophe. The apostrophe is important and I would rather adjust the code than change worksheet names. Is there any way around it?

You already know how to escape double quotes in a string. Single quotes are escaped the same way.
Application.OnTime printTaskTimers(UBound(printTaskTimers)), _
"'PrintTask """ & Replace(cRng.Value, "'", "''") & """, " & cRng.Offset(0, 1).Value & " '"

Related

Inserting semicolon between two quotation marks in VBA

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

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.

read and write a file in vbscript by escaping special characters

I want to copy a text file to another by escaping the special characters in the text and enclosing the output withing double quotes
For example. consider the input file to be:
ABC"hi%"
bcd
i want the output to be
"ABC" + "" + "hi%" + "" +vbCRLF + "bcd"
in a single line.
the script should dynamically add carriage return and linefeed at the end of each line in the input text.
any help is appreciated.
thanks
You should be able to do this using a series of Replace() statements.
s = objTextStream.ReadAll()
s = Replace(s, """", """""")
s = Replace(s, vbTab, """ & vbTab & """)
s = Replace(s, vbCrLF, """ & vbCrLf & """)
....
' Add quotes around the whole string
s = """" & s & """"
Because of the need to concatenate, however, you could end up with some empty strings in the final result. For example, if you had consecutive tabs, your output string would look something like this:
"some text" & vbTab & "" & vbTab & "" & vbCrLf & "Line2"
So you may have to do one final Replace() to get rid of those:
s = Replace(s, "& """" &", "&")

Applescript - Escaping Quotes When Setting Excel Formula

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)

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