-1

I have a variable for the string tag:some text that is stored in a cell. How can I modify the string that is stored in the variable so that it is interpreted as tag:\"some text\" (retaining the escape characters)?

Solution: s = Left(s, 4) & "\" & Chr(34) & Right(s, Len(s) - 4) & Chr(34)

user2319146
  • 371
  • 1
  • 11

3 Answers3

0

use chr(34) to append the quote to the string

dim s as string
s = "tag:\" & Chr(34) & "some text\" & Chr(34)
0

I am not completely understanding the question, but it seems that you are trying to make a string "some text" into "\"some text\""? If so, you can use CHR(34) as the double quotes. so it would look like

"\" & CHR(34) & "some text\" & CHR(34)
SRT HellKitty
  • 577
  • 3
  • 10
  • The string is stored in a cell, so I need to modify whatever string is stored in the variable. – user2319146 Nov 01 '18 at 19:58
  • okay, so string = "\" & CHR(34) & Cells(row, column).Value & CHR(34) – SRT HellKitty Nov 01 '18 at 20:01
  • That will put `\"` before `tag:`. – user2319146 Nov 01 '18 at 20:14
  • if that is always going to be a leading 4 characters in the string you can use [LEFT()](https://www.techonthenet.com/excel/formulas/left.php) and [RIGHT()](https://www.techonthenet.com/excel/formulas/left.php) functions to get only the first 4 characters and then everything from 5 to the end. – SRT HellKitty Nov 01 '18 at 20:20
0

Double Quotes are escaped by adding an extra Double Quotes or using Chr(34), the ascii char number for a Double Quote.

I usually build my strings in the Immediate Window

"tag:""some text"""
"tag:" & Chr(34) & "some text" & Chr(34)

If I have to escape many double quotes copy the string to the Clipboard and process it there:

With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    .GetFromClipboard
    .SetText Replace(.GetText, Chr(34), String(2, 34))
    .PutInClipboard
End With

enter image description here

TinMan
  • 6,624
  • 2
  • 10
  • 20