1

I'm creating a function that inputs a formula into a cell. But I keep on receiving a syntax error. I know this is due to the quotation marks. I was wondering how I would go about including the quotation marks of the formula in the function without causing a syntax error.

Any help would be grateful.

Sub codedump()


      Range("A1").Value = "=Dump(GoogleAnalytics("ga:78770227", "ga:visits", "2014-05-28", "2014-06-26", "","", "", "", 500, FALSE, FALSE))"

End Sub
Community
  • 1
  • 1
user3783607
  • 19
  • 1
  • 1
  • 5
  • Escape it with two double quotes. So a string with a quotation mark would look like `""""` – Elias Jun 27 '14 at 14:57

3 Answers3

4

Use escaping

For example, "" gives a quotation mark. Otherwise, use char codes

Sub codedump()

      Range("A1").Value = "=Dump(GoogleAnalytics(""ga:78770227"", ""ga:visits"", ""2014-05-28"", ""2014-06-26"", """","""", """", """", 500, FALSE, FALSE))"

End Sub

from http://msdn.microsoft.com/en-us/library/267k4fw5(v=vs.85).aspx

Alternatives:

'Escaping
Private Sub InsertQuote()
   TextBox1.Text = "She said, ""You deserve a treat!"" "
End Sub

'Character code
Private Sub InsertAscii()
   TextBox1.Text = "She said, " & Chr(34) & "You deserve a treat!" & Chr(34)
End Sub

'Defined string
Const quote As String = """"
TextBox1.Text = "She said, " & quote & "You deserve a treat!" & quote
JustinJDavies
  • 2,663
  • 4
  • 30
  • 52
0

You'll need to wrap your quotation marks in quotation marks to get them to appear on the sheet. Something like:

Range("A1").Value = "=Dump(GoogleAnalytics(""ga:78770227"", ""ga:visits"", ""2014-05-28"", ""2014-06-26"", """","""", """", """", 500, FALSE, FALSE))"

should do the trick.

sous2817
  • 3,915
  • 2
  • 33
  • 34
0

Use

 Chr(34) & Chr(34)

in place of

 ""

34 is the ASCII character code for " and VBA will convert Chr(34) to " when it builds your string.

asp8811
  • 793
  • 8
  • 14
  • 2
    VBA uses Unicode so ASCII character codes are not relevant. Of course, by design, Unicode is a superset of ASCII and uses the same character codes for the common characters. – Tom Blodget Jun 27 '14 at 16:51
  • Good to know. I usually just use the ASCII list because it's smaller and I don't need much other than what is in it. Thanks. – asp8811 Jun 27 '14 at 17:04