New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Import data from multiple pages of a website into a single Excel sheet

Macros, VBA, Excel Automation, etc.

Import data from multiple pages of a website into a single Excel sheet

Postby trainheavy » Mon Feb 01, 2016 11:06 am

Hi,

I'm trying to figure out a way to query data for batters vs left hand and batters vs right hand. The problem is, it only lists 40 per page and I don't know how to query all the pages onto a single page in excel. This is what I'm referring to: http://espn.go.com/mlb/stats/batting/_/ ... fied/false. Any help would be greatly appreciated.

This is the query I came up with - it cycles through but no data actually appears:

Code: Select all
Sub BatterVsLeftHand()
Dim nextRow As Integer, n As Integer
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
For n = 1 To 441 Step 40
Application.StatusBar = "Processing Page " & n
nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://espn.go.com/mlb/stats/batting/_/split/31/count/" & n & "/qualified/false", _
Destination:=Range("A" & nextRow))
.Name = "mlb/stats/batting/_/split/31/count/440/qualified/false"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "22"
.WebPreFormattedTextToColumns = False
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ThisWorkbook.Save
Next n
Application.StatusBar = False
End Sub
  • 0

trainheavy
Rookie
 
Posts: 3
Joined: Feb 1, 2016
Reputation: 0
Excel Version: 2013

Re: Import data from multiple pages of a website into a single Excel sheet

Postby NoSparks » Mon Feb 01, 2016 1:03 pm

This works for me.
All I did was use the macro recorder while manually getting the first page and insert your variables into the recorded macro to look after the whole thing.
Code: Select all
Option Explicit

Sub Macro1()
'
' Macro1 Macro
'
    Dim nextRow As Integer
    Dim n As Integer
   
Application.ScreenUpdating = False
Application.DisplayStatusBar = True

For n = 1 To 441 Step 40
    Application.StatusBar = "Processing Page " & n
    nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
   
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://espn.go.com/mlb/stats/batting/_/split/31/count/1/qualified/false", _
        Destination:=Range("$A$" & nextRow))
        .Name = "false"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    'ThisWorkbook.Save
Next n
Application.StatusBar = False
End Sub
  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Re: Import data from multiple pages of a website into a single Excel sheet

Postby trainheavy » Mon Feb 01, 2016 4:41 pm

It does pull in the data, but strangely some pages pull in and paste in other columns. For example if you replace /1/ with /41/ it doesn't pull the data into column A. But if I use /81/ it will. I can't quite figure out why it varies all over the place.

NoSparks wrote:This works for me.
All I did was use the macro recorder while manually getting the first page and insert your variables into the recorded macro to look after the whole thing.
Code: Select all
Option Explicit

Sub Macro1()
'
' Macro1 Macro
'
    Dim nextRow As Integer
    Dim n As Integer
   
Application.ScreenUpdating = False
Application.DisplayStatusBar = True

For n = 1 To 441 Step 40
    Application.StatusBar = "Processing Page " & n
    nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
   
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://espn.go.com/mlb/stats/batting/_/split/31/count/1/qualified/false", _
        Destination:=Range("$A$" & nextRow))
        .Name = "false"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    'ThisWorkbook.Save
Next n
Application.StatusBar = False
End Sub
  • 0

trainheavy
Rookie
 
Posts: 3
Joined: Feb 1, 2016
Reputation: 0
Excel Version: 2013

Re: Import data from multiple pages of a website into a single Excel sheet

Postby NoSparks » Mon Feb 01, 2016 8:13 pm

Change this line
Code: Select all
nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
to use column "B" instead of column "A".
  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Re: Import data from multiple pages of a website into a single Excel sheet

Postby trainheavy » Tue Feb 02, 2016 8:35 am

That did the trick - thanks!

NoSparks wrote:Change this line
Code: Select all
nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
to use column "B" instead of column "A".
  • 0

trainheavy
Rookie
 
Posts: 3
Joined: Feb 1, 2016
Reputation: 0
Excel Version: 2013

Re: Import data from multiple pages of a website into a single Excel sheet

Postby pecoflyer » Tue Feb 02, 2016 8:40 am

@ trainheavy

Please don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.
  • 0

A relevant topic title helps get faster and more answers
pecoflyer
Moderator
 
Posts: 1274
Joined: Jan 24, 2012
Location: Belgium
Reputation: 39
Excel Version: 2003/2007/2010


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 38 guests

cron