title: Export Excel Selected Range to Json
body: <p>I am trying to export a selected range to json and repeatedly run into the same problems receiving error messages &quot;Sub or Function not defined&quot;.</p>
  <pre><code>Sub PrintJson()
  Dim fs As Object
  Dim jsonfile
  Dim rangetoexport As Range
  Dim rowcounter As Long
  Dim columncounter As Long
  Dim linedata As String
  Dim path As String
  Dim fname As String
  
  Set rangetoexport = Sheets(&quot;Marketing&quot;).Range(&quot;C6&quot;, Range(&quot;C6&quot;).End(xlDown).End(xlToRight))
  
  path = ThisWorkbook.path &amp; &quot;\&quot;
  fname = &quot;export.json&quot;
  
  Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
  Set jsonfile = fs.CreateTextFile(path &amp; fname, True)
  
  linedata = &quot;{&quot;&quot;Output&quot;&quot;: [&quot;
  jsonfile.WriteLine linedata
  For rowcounter = 2 To rangetoexport.Rows.Count
      linedata = &quot;&quot;
      For columncounter = 1 To rangetoexport.Columns.Count
          linedata = linedata &amp; &quot;&quot;&quot;&quot; &amp; Replace(rangetoexport.Cells(1, columncounter), &quot;&quot;&quot;&quot;, &quot;\&quot;&quot;&quot;) &amp; &quot;&quot;&quot;&quot; &amp; &quot;:&quot; &amp; &quot;&quot;&quot;&quot; _
          &amp; Replace(rangetoexport.Cells(rowcounter, columncounter), &quot;&quot;&quot;&quot;, &quot;\&quot;&quot;&quot;) &amp; &quot;&quot;&quot;&quot; &amp; &quot;,&quot;
      Next
      linedata = Left(linedata, Len(linedata) - 1)
      If rowcounter = rangetoexport.Rows.Count Then
          linedata = &quot;{&quot; &amp; linedata &amp; &quot;}&quot;
      Else
          linedata = &quot;{&quot; &amp; linedata &amp; &quot;},&quot;
      End If
  
      jsonfile.WriteLine linedata
  Next
  linedata = &quot;]}&quot;
  jsonfile.WriteLine linedata
  jsonfile.Close
  
  Set fs = Nothing
  
  End Sub
  </code></pre>
  <p>I am really new to VBA and just learning. But this confuses me, so any help appreciated.</p>