Consulting

Results 1 to 3 of 3

Thread: Holding the formatting on a table from a website

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location

    Holding the formatting on a table from a website

    I have a code that pulls a table from a website with many of the cells in the table formatted as:

    1 - 3

    1 - 9

    9 - 4

    etc.

    When the table is pulled and added to the worksheet, excel insists on formatting those entries as dates.

    Question is, is there a way to stop that. I've tried to put "Format" in front of the querry table add line and " @ " after to format as text but that errors out. Also tried to just put " Cells.Numberformat= "@" " just before the table is pulled (and if I stop there and go to the WS and type in, say "9 - 4", it stays that way), but when it pulls the table from the website it overwrites the formatting...I guess...???

    Am I just putting it in the wrong place, or is there another way?

    Sub Pull_NCAA_Football_Standings()
    Sheets.Add before:=Sheets(1)
        Sheets(1).Name = "NCAAF Standings"
    Sheets("NCAAF Standings").Activate
      With ActiveSheet.QueryTables.Add("URL;http://espn.go.com/college-football/standings", Range("A1"))
        .Refresh False
        .PreserveFormatting = True  ' <---I've tried true and false here with no change
      End With
    End Sub
    BTW, if I put the cells formatting after the table is pulled, it changes each entry to the 5 digit "basic date" format (or whatever you call it, lol ).

    Any help is greatly appreciated.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Date formats are annoyingly intrusive and persistent!
    Try this
    [VBA]
    Sub Pull_NCAA_Football_Standings()
    Dim rng As Range
    Sheets.Add before:=Sheets(1)
    Sheets(1).Name = "NCAAF Standings"
    Sheets("NCAAF Standings").Activate
    With ActiveSheet.QueryTables.Add("URL;http://espn.go.com/college-football/standings", Range("A1"))
    .Refresh False
    .PreserveFormatting = True ' <---I've tried true and false here with no change
    End With
    With ActiveSheet
    Set rng = Intersect(.Columns("B:C"), .UsedRange)
    For Each cel In rng
    If IsDate(cel) Then
    txt = cel
    cel.ClearContents
    cel.NumberFormat = "General"
    cel.Value = "'" & Month(txt) & " - " & Day(txt)

    End If
    Next
    End With
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    Thank you so much for the help...

    Date formats are annoying indeed.

    OK, tried that and it converts MOST of them just right. A few of the teams have a record of 0 - something...0 - 8 is one of them I think. When it changes that one it makes it 8 - 1, which is confusing to me because if I remember correctly, before when it pulled the table the winless teams didnt change to dates...it stayed as 0 - 8 which is not a date format (that I know of at least) but yet the further code you gave (brilliant by the way) still decides that it IS a date, and changes it to 8 - 1.

    So....where the H%#L does it get the 1?? I can see it flipping it around...but changing the 0 to a 1 really leaves me wondering.
    Last edited by jproffer; 12-08-2009 at 08:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •