MacOS - download text from URL where destination is a string (not Range in sheet)

hilss

Active Member
Joined
May 22, 2007
Messages
379
Hello,
I'm a Mac user (currently on macOS Catalina Version 10.15.3). I'm using MS Excel for Mac (Version 16.34).

In my VBA code, I'm using the QueryTables.Add method to download data from a site. I pass in the connection (URL), and the destination (Range("A1")).
The text (JSON) has numerous lines. So it gets pasted in cells A1 through A19. In addition, it seems to be missing some characters (like double quotes, etc.).

Instead of setting the destination to cell A1, can I set it to be a string object? Or can I save it to a text file locally?

VBA Code:
Sub DownloadData()

    Dim jsonText As String
    Dim jsonObject As Object

    Dim destSheet As Worksheet
    Set destSheet = Application.Worksheets("DataSheet")

    destSheet.Rows.ClearContents
    destSheet.Activate
    With destSheet.QueryTables.Add(Connection:= _
        "URL;I_removed_the_URL_from_here_bcause_I_am_not_sure_about_the_mrExcels_policies" _
        , Destination:=Range("$A$1"))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

    i = 0
    Do Until Len(destSheet.Range("A1").Offset(i, 0)) < 1
        jsonText = jsonText + destSheet.Range("A1").Offset(i, 0)
        i = i + 1
    Loop

    destSheet.Rows.ClearContents
' because the data is missing " in certain spots, the parser fails.
    Set jsonObject = JsonConverter.ParseJson(jsonText)
' I will do more stuff with the jsonObject here....

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
found a solution here:



is there a way to mark this as "solved" ? Thanks
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top