How to import .xls file from web with VBA using querytables?

imnewhere

New Member
Joined
Mar 20, 2019
Messages
2
Hello all,

I'm getting "Invalid Query" on .Refresh BackgroundQuery:=False when stepping through the code. Is there a way to import .xls files from the web using querytables? I've seen it done for .csv and I'm trying to adapt that code to xls.

Thank you for helping!

Sub XLSImport()

Dim ImportSht As Worksheet
Set ImportSht = Sheet6

With Application
.ScreenUpdating = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
End With

ImportSht.Cells.ClearContents

Dim web As Object
Set web = CreateObject("Microsoft.XMLHTTP")



start:
web.Open "GET", "https://docs.misoenergy.org/marketreports/20190315_sr_nd_is.xls", False
web.send

If web.Status = "200" Then

With ImportSht.QueryTables.Add(Connection:="URL;https://docs.misoenergy.org/marketreports/20190315_sr_nd_is.xls" _
, Destination:=ImportSht.Range("A1"))
.Name = "XLS_IMPORT"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = False
.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
End If


With Application
.ScreenUpdating = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
End With

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to the Board

Code:
Sub Import_to_Sheet()
Dim ab, sp$
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://docs.misoenergy.org/marketreports/20190315_sr_nd_is.xls"
    .Send
    ab = .responseBody                     ' Get binary content
    sp = ThisWorkbook.Path & "\temp.xls"
End With
With CreateObject("ADODB.Stream")          ' Save binary content to xls file
    .Type = 1
    .Open
    .Write ab
    .SaveToFile sp, 2
    .Close
End With
With Workbooks.Open(sp, , True)
    ab = .Worksheets(1).UsedRange.Value     ' Get values to array
    .Saved = True
    .Close
End With
CreateObject("Scripting.FileSystemObject").DeleteFile sp, True
' Insert array to worksheet
ThisWorkbook.Sheets("Sheet2").Cells(1, 1).Resize(UBound(ab, 1), UBound(ab, 2)).Value = ab
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,517
Members
448,968
Latest member
Ajax40

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