VBA Excel: Web Page and QueryTable
Solvedyg_be Posted messages 23437 Registration date Status Contributor Last intervention -
Hello,
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
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
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 = "@"
-
-
-
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).
-
-
With my test HTML file, it works by simply setting it to text format.
Cells.Clear Cells.NumberFormat = "@" Set QT = ActiveSheet.QueryTables.Add(Connection:="URL;" & Url, _ Destination:=Range("A1")) ' configure the QueryTable... With QT .WebSelectionType = xlEntirePage '... to select the entire page '.PreserveFormatting = False '.WebDisableDateRecognition = False .Refresh BackgroundQuery:=False '... to refresh the data and paste it into the sheetone <br> 1-14 <br> three
-