Search notes:

VBA module ExcelRange - function excelRangeToJson

The function excelRangeToJson from the VBA module ExcelRange converts a range to a JSON representation.

Test case

option explicit

sub main() ' {

    cells(2,2) = "num" : cells(2, 3) = "txt": cells(2, 4) = "dat"                 : cells(2,5) = "msc"
    cells(3,2) =    1  : cells(3, 3) = "two": cells(3, 4) = #2020-08-28#          :
    cells(4,2) =  2.2  : cells(4, 3) =    2 : cells(4, 4) = #2021-08-28 22:23:24# : cells(4,5) = ""

    dim jsn as string
    jsn = excelRangeToJson(range(cells(2,2), cells(4,5)))

    if jsn <> "[[""num"",""txt"",""dat"",""msc""],[1,""two"",""2020-08-28T00:00:00"",null],[2.2,2,""2021-08-28T22:23:24"",null]]" then
       msgBox "jsn = " & jsn
    end if

end sub ' }
Github repository VBAModules, path: /_test/Office/xls-rangeToJson.vb
<job>
<script language="VBScript" src="../VBS-MS-Office-App-Creator/create-MS-Office-app.vbs" />
<script language="VBScript">

   option explicit

   dim app
   dim xls
   set xls = createOfficeApp("excel", currentDir() & "created.xlsm")
   if xls is nothing then ' {
      wscript.echo "Could not create excel worksheet."
      wscript.quit -1
   end if ' }

   set app = xls.application

   insertModule app, currentDir() & "../../Office/ExcelRange.vb"         , "ExcelRange"   , 1
   insertModule app, currentDir() & "../../Common/Date-Time/dateTime.vb" , "dateTime"     , 1
   insertModule app, currentDir() & "../../Common/JSON.vb"               , "json"         , 1
   insertModule app, currentDir() & "../../Common/Text/StringBuffer.cls" , "StringBuffer" , 2
   insertModule app, currentDir() & "xls-rangeToJson.vb"                 , "func"         , 1

   app.run "main"

   xls.save

   createObject("WScript.Shell").appActivate(app.caption)

</script> </job>
Github repository VBAModules, path: /_test/Office/xls-rangeToJson.wsf

See also

Turn the selected range into SQL insert statements.

Index