VBA Excel: Web Page and QueryTable

Solved

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

  • Votes
  • Date
  • Descending date
  1. 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 = "@" 
    1. Contributor

      You could also have tested it simply by assigning a value like this:

       Cells.Clear [a5] = "1-14" Cells.NumberFormat = "@" [a6] = "1-14" 

      You would have noticed that it had nothing to do with the fact that the source was a web page.

    2. Thank you for your response!
      1) If I set the cells to text format before the Qt set, it doesn't change anything.
      2) If I do it afterwards, the janv-14 obviously transforms into 41640...
      Is there no configuration parameter for QT?

    3. Contributor
      @mikel831

      Perhaps, simply,

       With QT .WebSelectionType = xlEntirePage '... to select the entire page .PreserveFormatting = True .Refresh BackgroundQuery:=False 
    4. @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?