+ Reply to Thread
Results 1 to 3 of 3

How to modify a VBA to place all data on one page instead of a new page each query

Hybrid View

  1. #1
    Registered User
    Join Date
    10-26-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2016
    Posts
    56

    How to modify a VBA to place all data on one page instead of a new page each query

    Hello! Please help me, this is the code I have so far which I have found online.

    Private Const URL_TEMPLATE As String = "URL;http://www.nhl.com/ice/playerstats.htm?fetchKey=20142ALLSASALL&viewName=assists&sort=player.bioFirstNameLastName&pg={0}"
    Private Const NUMBER_OF_PAGES As Byte = 15
    
    Sub test()
        Dim page As Byte
        Dim queryTableObject As QueryTable
        Dim url As String
    
        For page = 1 To NUMBER_OF_PAGES
            url = VBA.Strings.Replace(URL_TEMPLATE, "{0}", page)
            Set queryTableObject = ActiveSheet.QueryTables.Add(Connection:=url, Destination:=ThisWorkbook.Worksheets.Add.[a1])
            queryTableObject.WebSelectionType = xlSpecifiedTables
            queryTableObject.WebTables = "6"
            queryTableObject.Refresh
        Next page
    
    End Sub
    The only modification I have made is change the WebTables and the target URL. How can I edit this so that each new page query will add the returned data on the same sheet, underneath the previous page?

    As it is now it creates a new worksheet for every page query. Thanks!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,252

    Re: How to modify a VBA to place all data on one page instead of a new page each query

    This gets them all on the same worksheet.

    Option Explicit
    
    Private Const URL_TEMPLATE As String = "URL;http://www.nhl.com/ice/playerstats.htm?fetchKey=20142ALLSASALL&viewName=assists&sort=player.bioFirstNameLastName&pg={0}"
    Private Const NUMBER_OF_PAGES As Byte = 15
    
    Sub test1()
    ' down the page ...
    Dim page As Long
    Dim queryTableObject As QueryTable
    Dim url As String
    
    For page = 1 To NUMBER_OF_PAGES
        url = VBA.Strings.Replace(URL_TEMPLATE, "{0}", page)
        Set queryTableObject = _
            ActiveSheet.QueryTables.Add(Connection:=url, _
            Destination:=ThisWorkbook.ActiveSheet.Cells((page - 1) * 40 + 1, 1))
        queryTableObject.WebSelectionType = _
            xlSpecifiedTables
        queryTableObject.WebTables = "6"
        queryTableObject.Refresh
        
    Next page
    
    End Sub
    
    Sub test2()
    ' across the page
    Dim page As Long
    Dim queryTableObject As QueryTable
    Dim url As String
    
    For page = 1 To NUMBER_OF_PAGES
        url = VBA.Strings.Replace(URL_TEMPLATE, "{0}", page)
        Set queryTableObject = _
            ActiveSheet.QueryTables.Add(Connection:=url, _
            Destination:=ThisWorkbook.ActiveSheet.Cells(1, (page - 1) * 26 + 1))
        queryTableObject.WebSelectionType = _
            xlSpecifiedTables
        queryTableObject.WebTables = "6"
        queryTableObject.Refresh
        
    Next page
    
    End Sub


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,252

    Re: How to modify a VBA to place all data on one page instead of a new page each query

    Slight refinement. Every 36 rows and fit to page width.

    Option Explicit
    
    Private Const URL_TEMPLATE As String = "URL;http://www.nhl.com/ice/playerstats.htm?fetchKey=20142ALLSASALL&viewName=assists&sort=player.bioFirstNameLastName&pg={0}"
    Private Const NUMBER_OF_PAGES As Byte = 15
    
    Sub test3()
    ' down the page ...
    Dim page As Long
    Dim queryTableObject As QueryTable
    Dim url As String
    
    For page = 1 To NUMBER_OF_PAGES
        url = VBA.Strings.Replace(URL_TEMPLATE, "{0}", page)
        Set queryTableObject = _
            ActiveSheet.QueryTables.Add(Connection:=url, _
            Destination:=ThisWorkbook.ActiveSheet.Cells((page - 1) * 36 + 1, 1))
        queryTableObject.WebSelectionType = _
            xlSpecifiedTables
        queryTableObject.WebTables = "6"
        queryTableObject.Refresh
    Next page
    
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .PaperSize = xlPaperA4
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
    End With
    
    End Sub

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 12-31-2012, 12:03 PM
  2. Import data from web page- I cannot use direct function as web query.
    By manmehdiam in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-01-2012, 07:09 PM
  3. [SOLVED] Web query to download web page data to excel in text format
    By panditji in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-15-2012, 12:30 AM
  4. Web Query and Page Source - Importing Data
    By exc4libur in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2011, 11:52 AM
  5. Query for data from a web page
    By i2trader in forum Excel General
    Replies: 3
    Last Post: 10-27-2009, 02:42 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1