VBA Excel: Web Page and QueryTable
SolvedHello,
The following piece of code
' Copy the web page into "Sheet2"
Sheets("Sheet2").Select
Cells.Clear
' add a QueryTable
Set QT = ActiveSheet.QueryTables.Add(Connection:="URL;" & URL,
Destination:=Range("A1"))
' configure the QueryTable...
With QT
.WebSelectionType = xlEntirePage '... to select the entire page
.Refresh BackgroundQuery:=False '... to refresh the data and paste it into the sheet
End With
' release the QueryTable object
Set QT = Nothing
......
copies an entire web page into Sheet2 starting from A1 and it works satisfactorily for my needs. However, some areas of the web page contain indications like 1-14 (for example) and are imported into the Excel cells as a date format like Jan-14 ... Is it possible to avoid this issue and get back 1-14?
Windows / Chrome 124.0.0.0
1 answer
-
Contributor
Hello,
You can see that you have the same behavior if you manually enter "1-14" into a cell.
To prevent Excel from interpreting the data, you must first set the cells to "Text" format.
Cells.Clear Cells.NumberFormat = "@"
-
-
-
Contributor@mikel831
I think there is a setting that allows you to preserve the format of the cells.
It would be more of a setting accessible through the destination, rather than through the connection.
I suggest you test manually to find this setting, then automate if necessary (the VBA could simply use an existing parameter).
-
Contributor@mikel831
Perhaps, simply,
With QT .WebSelectionType = xlEntirePage '... to select the entire page .PreserveFormatting = True .Refresh BackgroundQuery:=False
-
@yg_be
I tried your answer <5: it doesn't change anything...
If there is a way via VBA to know the format of a cell, I can maybe try to reconstruct the text 1-14 if it's a date in the form of Jan-14?
-