Help with Macro to sort BBC football fixtures

tommyleinen

Board Regular
Joined
Aug 22, 2009
Messages
74
Hi all,

The BBC updated their sports pages overnight to a new format which puts my football predictor leagues in a bit of a pickle due to the way the scores etc all run on formulas driven by the BBC fixture format. I think you might call that an achilles heal - or just plain stupidity!

What I need is a macro to sort the new style into the old style, so for example if I paste values of the current format down column A (due to their format this would take up columns A-C). I then need something that can sort them into one column, be it D, or insert a new column A for tidiness' sake. You can find the fixture list here: http://www.bbc.co.uk/sport/football/league-one/fixtures

If you highlight, copy, andtry pasting the values into excel you will see what I mean, the old format is a single cell: Walsall v Notts County, 19:45

Also, at the top of each day's fixtures was the day in the format below:
Tuesday, 31 January 2012
Walsall v Notts County, 19:45

At the end of each day, it needs to skip 2 cells and start the next day's fixtures.

I know a little vba, but not really enough for this to take less than about 10hrs!

If any talented soul could please take a look at this I would be very grateful! ;)
 
In the code above, where show is present in the line is it possible to modify it to delete just the cells in that row from A:D? This would leave me room on the right hand side for a formula to format as above.

If I am able to put formulas in down the right hand side to convert the format for columns A:C, what I would like is for the code to also insert 3 empty cells A:C just before the date changes, and 3 empty cells A:C on the top line eg, the output will look like:

<TABLE style="WIDTH: 243pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=324><COLGROUP><COL style="WIDTH: 124pt; mso-width-source: userset; mso-width-alt: 6034" width=165><COL style="WIDTH: 71pt; mso-width-source: userset; mso-width-alt: 3474" width=95><COL style="WIDTH: 48pt" width=64><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 124pt; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl65 height=20 width=165>(Blank Row)</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 71pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" width=95></TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 48pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" width=64></TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Man Utd v Liverpool</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 12:45</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Blackburn v QPR</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 15:00</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Bolton v Wigan</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 15:00</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Everton v Chelsea</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 15:00</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Fulham v Stoke</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 15:00</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Sunderland v Arsenal</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 15:00</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Swansea v Norwich</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 15:00</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Tottenham v Newcastle</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sat 11 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 17:30</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl65 height=20>(Blank Row)</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"></TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"></TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl65 height=20>(Blank Row)</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"></TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"></TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl65 height=20>(Blank Row)</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"></TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"></TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Wolves v West Brom</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sun 12 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 13:30</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=20>Aston Villa v Man City</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> Sun 12 Feb</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8"> 16:00</TD></TR></TBODY></TABLE>

Bearing in mind I will have formulas in the area to the right (E1:G1000) is this possible?

Thankyou in advance.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
See if this output is any use to you.

The Main() procedure controls the process.

Code:
[COLOR=darkblue]Sub[/COLOR] Main()
   ImportFromWeb
   TidyUp
   SeparateDates
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

ImportFromWeb()
I found a query on the web which I adjusted for our needs.
Points of interest are the id of the table we are extracting,
And the number of tables (there are four in this example).

This imports the data into Sheet2. I have declared this sheet name as a constant for ease of editing.

TidyUp()
I have assumed Sheet1 is your template.
This procedure strips out unwanted content and copies and pastes the processed data into Sheet1.


SeparatedDates()
This procedure - be aware gaps between rows can mess up formula - inserts three blank rows between dates.


To Use
Open a new Excel workbook.
Press ALt+Tab to open the VBA Editor window.
Copy and paste all the procedures into the ThisWorkbook module in the Project Window on the Left hand side.
Run the Main() procedure

Code:
[COLOR=darkblue]Const[/COLOR] sheetName = "[COLOR=red]Sheet2[/COLOR]"
 
 
[COLOR=darkblue]Sub[/COLOR] Main()
   ImportFromWeb
   TidyUp
   SeparateDates
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=#00008b][/COLOR] 
[COLOR=#00008b][/COLOR] 
[COLOR=darkblue]Sub[/COLOR] SeparateDates()
   [COLOR=darkblue]Dim[/COLOR] lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets("[COLOR=red]Sheet1[/COLOR]")
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      [COLOR=darkblue]For[/COLOR] i = lr [COLOR=darkblue]To[/COLOR] 1 [COLOR=darkblue]Step[/COLOR] -1
 
         [COLOR=darkblue]If[/COLOR] i <> 1 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] .Range("A" & i).Value <> .Range("A" & i - 1).Value [COLOR=darkblue]Then[/COLOR]
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]Next[/COLOR] i
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Sub[/COLOR] TidyUp()
   [COLOR=darkblue]Dim[/COLOR] lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] txt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   rw = 1
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      .Range("E1").Value = "Date"
      .Range("F1").Value = "Fixture"
      .Range("G1").Value = "Kick Off"
 
      lr = .Range("A" & Rows.Count).End(xlUp).Row
      [COLOR=darkblue]For[/COLOR] i = 4 [COLOR=darkblue]To[/COLOR] lr
         [COLOR=darkblue]If[/COLOR] .Range("B" & i).Value <> "" [COLOR=darkblue]Then[/COLOR]
            txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
            rw = rw + 1
            .Range("E" & rw).Value = .Range("B" & i).Value
            .Range("F" & rw).Value = txt
            .Range("G" & rw).Value = .Range("C" & i).Text
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]Next[/COLOR] i
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      .Columns("A:D").Delete shift:=xlToLeft
      .Columns("A:C").AutoFit
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'copy and paste[/COLOR]
   Sheets(sheetName).UsedRange.Copy _
      Destination:=Sheets("[COLOR=red]Sheet1[/COLOR]").Range("A1")
[COLOR=darkblue]End[/COLOR] Sub
 
 
 
Sub ImportFromWeb()
[COLOR=darkblue]With[/COLOR] Sheets(sheetName).QueryTables.Add(Connection:= _
"URL;http://www.bbc.co.uk/sport/football/league-one/fixtures", _
   Destination:=Sheets(sheetName).Range("$A$1"))
.Name = "[COLOR=red]fixtures-data[/COLOR]"
.FieldNames = [COLOR=darkblue]True[/COLOR]
.RowNumbers = [COLOR=darkblue]False[/COLOR]
.FillAdjacentFormulas = [COLOR=darkblue]False[/COLOR]
.PreserveFormatting = [COLOR=darkblue]True[/COLOR]
.RefreshOnFileOpen = [COLOR=darkblue]False[/COLOR]
.BackgroundQuery = [COLOR=darkblue]True[/COLOR]
.RefreshStyle = xlInsertDeleteCells
.SavePassword = [COLOR=darkblue]False[/COLOR]
.SaveData = [COLOR=darkblue]True[/COLOR]
.AdjustColumnWidth = [COLOR=darkblue]True[/COLOR]
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "[COLOR=red]1,2,3,4[/COLOR]"
.WebPreFormattedTextToColumns = [COLOR=darkblue]True[/COLOR]
.WebConsecutiveDelimitersAsOne = [COLOR=darkblue]True[/COLOR]
.WebSingleBlockTextImport = [COLOR=darkblue]False[/COLOR]
.WebDisableDateRecognition = [COLOR=darkblue]False[/COLOR]
.WebDisableRedirections = [COLOR=darkblue]False[/COLOR]
.Refresh BackgroundQuery:=[COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
Sheets(sheetName).Columns(1).Delete shift:=xlToLeft
[COLOR=darkblue]End[/COLOR] Sub


Output - It will take about 10 seconds to run:

Excel 2007<TABLE style="BORDER-BOTTOM: #a6aab6 1px solid; BORDER-LEFT: #a6aab6 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-COLLAPSE: collapse; BORDER-TOP: #a6aab6 1px solid; BORDER-RIGHT: #a6aab6 1px solid" rules=all cellPadding=2><COLGROUP><COL style="BACKGROUND-COLOR: #e0e0f0" width=25><COL><COL><COL></COLGROUP><THEAD><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #e0e0f0; COLOR: #161120"><TH></TH><TH>A</TH><TH>B</TH><TH>C</TH></TR></THEAD><TBODY><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">5</TD><TD>Sat 11 Feb</TD><TD>Brentford v Oldham</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">6</TD><TD>Sat 11 Feb</TD><TD>Chesterfield v Charlton</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">7</TD><TD>Sat 11 Feb</TD><TD>Exeter v Sheff Wed</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">8</TD><TD>Sat 11 Feb</TD><TD>Hartlepool v Bournemouth</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR></TBODY></TABLE>
Sheet1
 
Upvote 0
See if this output is any use to you.

The Main() procedure controls the process.

Code:
[COLOR=darkblue]Sub[/COLOR] Main()
   ImportFromWeb
   TidyUp
   SeparateDates
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

ImportFromWeb()
I found a query on the web which I adjusted for our needs.
Points of interest are the id of the table we are extracting,
And the number of tables (there are four in this example).

This imports the data into Sheet2. I have declared this sheet name as a constant for ease of editing.

TidyUp()
I have assumed Sheet1 is your template.
This procedure strips out unwanted content and copies and pastes the processed data into Sheet1.


SeparatedDates()
This procedure - be aware gaps between rows can mess up formula - inserts three blank rows between dates.


To Use
Open a new Excel workbook.
Press ALt+Tab to open the VBA Editor window.
Copy and paste all the procedures into the ThisWorkbook module in the Project Window on the Left hand side.
Run the Main() procedure

Code:
[COLOR=darkblue]Const[/COLOR] sheetName = "[COLOR=red]Sheet2[/COLOR]"
 
 
[COLOR=darkblue]Sub[/COLOR] Main()
   ImportFromWeb
   TidyUp
   SeparateDates
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Sub[/COLOR] SeparateDates()
   [COLOR=darkblue]Dim[/COLOR] lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets("[COLOR=red]Sheet1[/COLOR]")
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      [COLOR=darkblue]For[/COLOR] i = lr [COLOR=darkblue]To[/COLOR] 1 [COLOR=darkblue]Step[/COLOR] -1
 
         [COLOR=darkblue]If[/COLOR] i <> 1 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] .Range("A" & i).Value <> .Range("A" & i - 1).Value [COLOR=darkblue]Then[/COLOR]
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]Next[/COLOR] i
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Sub[/COLOR] TidyUp()
   [COLOR=darkblue]Dim[/COLOR] lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] txt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   rw = 1
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      .Range("E1").Value = "Date"
      .Range("F1").Value = "Fixture"
      .Range("G1").Value = "Kick Off"
 
      lr = .Range("A" & Rows.Count).End(xlUp).Row
      [COLOR=darkblue]For[/COLOR] i = 4 [COLOR=darkblue]To[/COLOR] lr
         [COLOR=darkblue]If[/COLOR] .Range("B" & i).Value <> "" [COLOR=darkblue]Then[/COLOR]
            txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
            rw = rw + 1
            .Range("E" & rw).Value = .Range("B" & i).Value
            .Range("F" & rw).Value = txt
            .Range("G" & rw).Value = .Range("C" & i).Text
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]Next[/COLOR] i
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      .Columns("A:D").Delete shift:=xlToLeft
      .Columns("A:C").AutoFit
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'copy and paste[/COLOR]
   Sheets(sheetName).UsedRange.Copy _
      Destination:=Sheets("[COLOR=red]Sheet1[/COLOR]").Range("A1")
[COLOR=darkblue]End[/COLOR] Sub
 
 
 
Sub ImportFromWeb()
[COLOR=darkblue]With[/COLOR] Sheets(sheetName).QueryTables.Add(Connection:= _
"URL;http://www.bbc.co.uk/sport/football/league-one/fixtures", _
   Destination:=Sheets(sheetName).Range("$A$1"))
.Name = "[COLOR=red]fixtures-data[/COLOR]"
.FieldNames = [COLOR=darkblue]True[/COLOR]
.RowNumbers = [COLOR=darkblue]False[/COLOR]
.FillAdjacentFormulas = [COLOR=darkblue]False[/COLOR]
.PreserveFormatting = [COLOR=darkblue]True[/COLOR]
.RefreshOnFileOpen = [COLOR=darkblue]False[/COLOR]
.BackgroundQuery = [COLOR=darkblue]True[/COLOR]
.RefreshStyle = xlInsertDeleteCells
.SavePassword = [COLOR=darkblue]False[/COLOR]
.SaveData = [COLOR=darkblue]True[/COLOR]
.AdjustColumnWidth = [COLOR=darkblue]True[/COLOR]
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "[COLOR=red]1,2,3,4[/COLOR]"
.WebPreFormattedTextToColumns = [COLOR=darkblue]True[/COLOR]
.WebConsecutiveDelimitersAsOne = [COLOR=darkblue]True[/COLOR]
.WebSingleBlockTextImport = [COLOR=darkblue]False[/COLOR]
.WebDisableDateRecognition = [COLOR=darkblue]False[/COLOR]
.WebDisableRedirections = [COLOR=darkblue]False[/COLOR]
.Refresh BackgroundQuery:=[COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
Sheets(sheetName).Columns(1).Delete shift:=xlToLeft
[COLOR=darkblue]End[/COLOR] Sub


Output - It will take about 10 seconds to run:

Excel 2007<TABLE style="BORDER-BOTTOM: #a6aab6 1px solid; BORDER-LEFT: #a6aab6 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-COLLAPSE: collapse; BORDER-TOP: #a6aab6 1px solid; BORDER-RIGHT: #a6aab6 1px solid" rules=all cellPadding=2><COLGROUP><COL style="BACKGROUND-COLOR: #e0e0f0" width=25><COL><COL><COL></COLGROUP><THEAD><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #e0e0f0; COLOR: #161120"><TH></TH><TH>A</TH><TH>B</TH><TH>C</TH></TR></THEAD><TBODY><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">5</TD><TD>Sat 11 Feb</TD><TD>Brentford v Oldham</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">6</TD><TD>Sat 11 Feb</TD><TD>Chesterfield v Charlton</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">7</TD><TD>Sat 11 Feb</TD><TD>Exeter v Sheff Wed</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">8</TD><TD>Sat 11 Feb</TD><TD>Hartlepool v Bournemouth</TD><TD style="TEXT-ALIGN: right">15:00</TD></TR></TBODY></TABLE>

Sheet1


Wow! Thankyou Bertie, what a champ! No need to even open a browser!:cool:

That's brilliant. I am just having a play with it and seeing how it slots into my existing sheet. I notice it uses sheet 2, so as long as I import both sheets no other sheets in the destination workbook should be affected is that true? I am thinking I may run this as a seperate workbook actually so I can alt tab between them.

Also, I run 3 leagues, so I thought I would duplicate the code (using 2 different urls) and call them something different, using sheets 3 & 4 and 5 & 6 - does that sound workable? That means I can run them all at once. Apologies for omitting that info but I really didn't expect the solution to be so polished! Before now the fanciest it got was 3 hyperlinks and manual copy paste...

Thanks again :biggrin:
 
Upvote 0
Sheet2 is simply a data dump. It can either be deleted or the UsedRange cleared for reuse. I copied it to Sheet1 as I assumed this to be the template sheet.

As for you other leagues, consider the ImportFromWeb() procedure.

There are three things you would need to change, highlighted red below:

The URL.
obviously

The id of the table.
If you are using Google Chrome, right click on the table and select View Element. You are interested in the id or name of the element.
HTML:
<div class="fixtures-table full-table-medium" id="fixtures-data">

I believe you can also inspect the element using FireFox, but Internet Explorer you will have to trawl through the html to find it. Click View Source and good luck.

The number of tables, there were four on the original URL so this gave us.
.WebTables = "1,2,3,4"

I have highlighted red below where these changes would be made.
Code:
Sub ImportFromWeb()
With Sheets(sheetName).QueryTables.Add(Connection:= _
"URL;[COLOR="Red"]http://www.bbc.co.uk/sport/football/league-one/fixtures[/COLOR]", _
   Destination:=Sheets(sheetName).Range("$A$1"))
.Name = "[COLOR="red"]fixtures-data[/COLOR]"
.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 = "[COLOR="red"]1,2,3,4[/COLOR]"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Sheets(sheetName).Columns(1).Delete shift:=xlToLeft
End Sub

If you want to paste you other league into a new worksheet change the destination in the TidyUp() procedure.
Code:
'copy and paste
   Sheets(sheetName).UsedRange.Copy _
      Destination:=Sheets([COLOR="Red"]"Sheet1[/COLOR]").Range("A1")
End Sub
 
Upvote 0
Thanks Berite, here is your code (x3) for the 3 leagues, though only your original works correctly! It fails on the 2nd one with "Subscript out of range".

I basically duplicated the 3 stages of code, allocating them each 2 sheets to work with, so Premierleague is now on sheet1 (sheet2 as dump) ELO on sheet3 (sheet4 as dump) and SPL sheet5 (sheet6 as dump).

I added the extra stages to Main(), and checked the links and table references. Tables are all the same for each league, just the urls change.

I noticed a couple of things though which will add a spanner to the works:


  • BBC have now added a line saying some games are postponed (see ELO below)

shot.jpg



  • As the season draws to a close there will be less and less tables, eventually only one. Disregard this as I will just amend the webtables="1,2,3,4" in the last 3 months. Currently SPL only has 3 left.
Here is the amended code:



Const sheetName = "Sheet2"
Const sheetNameELO = "Sheet4"
Const sheetNameSPL = "Sheet6"


Sub Main()
ImportFromWeb
TidyUp
SeparateDates
ImportFromWebELO
TidyUpELO
SeparateDatesELO
ImportFromWebSPL
TidyUpSPL
SeparateDatesSPL
End Sub


Sub SeparateDates()
Dim lr As Long
Dim i As Long

With Sheets("Sheet1")
lr = .Range("A" & .Rows.Count).End(xlUp).Row
For i = lr To 1 Step -1

If i <> 1 Then
If .Range("A" & i).Value <> .Range("A" & i - 1).Value Then
Rows(i).Insert shift:=xlDown
Rows(i).Insert shift:=xlDown
Rows(i).Insert shift:=xlDown
End If
End If
Next i
End With
End Sub


Sub TidyUp()
Dim lr As Long
Dim i As Long
Dim txt As String
Dim rw As Long

rw = 1
With Sheets(sheetName)
.Range("E1").Value = "Date"
.Range("F1").Value = "Fixture"
.Range("G1").Value = "Kick Off"

lr = .Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To lr
If .Range("B" & i).Value <> "" Then
txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
rw = rw + 1
.Range("E" & rw).Value = .Range("B" & i).Value
.Range("F" & rw).Value = txt
.Range("G" & rw).Value = .Range("C" & i).Text
End If
Next i
End With

With Sheets(sheetName)
lr = .Range("A" & .Rows.Count).End(xlUp).Row
.Columns("A:D").Delete shift:=xlToLeft
.Columns("A:C").AutoFit
End With

'copy and paste
Sheets(sheetName).UsedRange.Copy _
Destination:=Sheets("Sheet1").Range("A1")
End Sub



Sub ImportFromWeb()
With Sheets(sheetName).QueryTables.Add(Connection:= _
"URL;http://www.bbc.co.uk/sport/football/premier-league/fixtures", _
Destination:=Sheets(sheetName).Range("$A$1"))
.Name = "fixtures-data"
.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 = "1,2,3,4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Sheets(sheetName).Columns(1).Delete shift:=xlToLeft
End Sub




Sub SeparateDatesELO()
Dim lr As Long
Dim i As Long

With Sheets("Sheet3")
lr = .Range("A" & .Rows.Count).End(xlUp).Row
For i = lr To 1 Step -1

If i <> 1 Then
If .Range("A" & i).Value <> .Range("A" & i - 1).Value Then
Rows(i).Insert shift:=xlDown
Rows(i).Insert shift:=xlDown
Rows(i).Insert shift:=xlDown
End If
End If
Next i
End With
End Sub


Sub TidyUpELO()
Dim lr As Long
Dim i As Long
Dim txt As String
Dim rw As Long

rw = 1
With Sheets(sheetNameELO)
.Range("E1").Value = "Date"
.Range("F1").Value = "Fixture"
.Range("G1").Value = "Kick Off"

lr = .Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To lr
If .Range("B" & i).Value <> "" Then
txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
rw = rw + 1
.Range("E" & rw).Value = .Range("B" & i).Value
.Range("F" & rw).Value = txt
.Range("G" & rw).Value = .Range("C" & i).Text
End If
Next i
End With

With Sheets(sheetNameELO)
lr = .Range("A" & .Rows.Count).End(xlUp).Row
.Columns("A:D").Delete shift:=xlToLeft
.Columns("A:C").AutoFit
End With

'copy and paste
Sheets(sheetNameELO).UsedRange.Copy _
Destination:=Sheets("Sheet3").Range("A1")
End Sub



Sub ImportFromWebELO()
With Sheets(sheetNameELO).QueryTables.Add(Connection:= _
"URL;http://www.bbc.co.uk/sport/football/league-one/fixtures", _
Destination:=Sheets(sheetNameELO).Range("$A$1"))
.Name = "fixtures-data"
.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 = "1,2,3,4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Sheets(sheetName2).Columns(1).Delete shift:=xlToLeft
End Sub



Sub SeparateDatesSPL()
Dim lr As Long
Dim i As Long

With Sheets("Sheet5")
lr = .Range("A" & .Rows.Count).End(xlUp).Row
For i = lr To 1 Step -1

If i <> 1 Then
If .Range("A" & i).Value <> .Range("A" & i - 1).Value Then
Rows(i).Insert shift:=xlDown
Rows(i).Insert shift:=xlDown
Rows(i).Insert shift:=xlDown
End If
End If
Next i
End With
End Sub


Sub TidyUpSPL()
Dim lr As Long
Dim i As Long
Dim txt As String
Dim rw As Long

rw = 1
With Sheets(sheetNameSPL)
.Range("E1").Value = "Date"
.Range("F1").Value = "Fixture"
.Range("G1").Value = "Kick Off"

lr = .Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To lr
If .Range("B" & i).Value <> "" Then
txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
rw = rw + 1
.Range("E" & rw).Value = .Range("B" & i).Value
.Range("F" & rw).Value = txt
.Range("G" & rw).Value = .Range("C" & i).Text
End If
Next i
End With

With Sheets(sheetNameSPL)
lr = .Range("A" & .Rows.Count).End(xlUp).Row
.Columns("A:D").Delete shift:=xlToLeft
.Columns("A:C").AutoFit
End With

'copy and paste
Sheets(sheetNameSPL).UsedRange.Copy _
Destination:=Sheets("Sheet5").Range("A1")
End Sub



Sub ImportFromWebSPL()
With Sheets(sheetNameSPL).QueryTables.Add(Connection:= _
"URL;http://www.bbc.co.uk/sport/football/scottish-premier/fixtures", _
Destination:=Sheets(sheetNameSPL).Range("$A$1"))
.Name = "fixtures-data"
.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 = "1,2,3"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Sheets(sheetNameSPL).Columns(1).Delete shift:=xlToLeft
End Sub
 
Upvote 0
Hi Tommy,

I am off out shortly to meet a freind for a wee refreshement, so it may be tomorrow before I can look at this again.

Can you post URL's to all leagues you want to download.

Here is what I plan to do
We only need the procedure I posted earleir, we can call it when we need it..
Set up an array of URL's and loop through it.
Probably have a Select Case statement assigning Leage Name(sheet name) and Number of tables,

As for the tables a more efficient approach would be to use a sheet as a template. This would be the sheet which had any formula in place.

Copy the template,
Rename it as the League Name
Copy the downloaded information into there.

Anything else you can think of, please post it along with the URL's

And I will see you later.

Bertie
 
Upvote 0
Hi Tommy,

Insert a worksheet and name it Template. This sheet will be copied for each league.

Insert a worksheet and name it Data. Lay it out as:
<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">URL</td><td style=";">League/Sheet Name</td><td style=";">#Tables</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">http://www.bbc.co.uk/sport/football/premier-league/fixtures</td><td style=";">EPL</td><td style="text-align: right;background-color: #FFFF00;;">1,2,3,4</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">http://www.bbc.co.uk/sport/football/league-one/fixtures</td><td style=";">nPower01</td><td style="text-align: right;background-color: #FFFF00;;">1,2,3,4</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">http://www.bbc.co.uk/sport/football/scottish-premier/fixtures</td><td style=";">SPL</td><td style="text-align: right;background-color: #FFFF00;;">1,2,3</td></tr></tbody></table><p style="width:2.4em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Data</p><br /><br />

NB column C, number of tables on the webpage, may need manual updating.

The code now loops through the Data sheet, copies and renames the Template, and downloads the information from the url.

The Main() procedure controls the code.
Code:
[color=darkblue]Sub[/color] Main()
   [color=darkblue]Dim[/color] rng [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] sUrl [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] sSheetName [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] sNumTables [color=darkblue]As[/color] [color=darkblue]String[/color]
   
   [color=darkblue]Set[/color] rng = Sheets("Data").Range("A2")
   [color=darkblue]Do[/color] [color=darkblue]Until[/color] rng = ""
      sUrl = rng.Value
      sSheetName = rng.Offset(, 1).Value
      sNumTables = rng.Offset(, 2).Value
      
      ImportFromWeb sUrl, sSheetName, sNumTables
      TidyUp sSheetName
      SeparateDates sSheetName
      
      [color=darkblue]Set[/color] rng = rng.Offset(1, 0)
   [color=darkblue]Loop[/color]

[color=darkblue]End[/color] [color=darkblue]Sub[/color]


[color=darkblue]Sub[/color] ImportFromWeb([color=darkblue]ByVal[/color] sUrl [color=darkblue]As[/color] [color=darkblue]String[/color], _
                  [color=darkblue]ByVal[/color] sSheetName [color=darkblue]As[/color] [color=darkblue]String[/color], _
                  [color=darkblue]ByVal[/color] sNumTables [color=darkblue]As[/color] [color=darkblue]String[/color])
   [color=green]'copy the template[/color]
   Sheets("Template").Copy After:=Worksheets(Worksheets.Count)
   ActiveSheet.Name = sSheetName
   
   [color=darkblue]With[/color] Sheets(sSheetName).QueryTables.Add(Connection:= _
      "URL;" & sUrl, _
      Destination:=Sheets(sSheetName).Range("$A$1"))
      .Name = "fixtures-data"
      .FieldNames = [color=darkblue]True[/color]
      .RowNumbers = [color=darkblue]False[/color]
      .FillAdjacentFormulas = [color=darkblue]False[/color]
      .PreserveFormatting = [color=darkblue]True[/color]
      .RefreshOnFileOpen = [color=darkblue]False[/color]
      .BackgroundQuery = [color=darkblue]True[/color]
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = [color=darkblue]False[/color]
      .SaveData = [color=darkblue]True[/color]
      .AdjustColumnWidth = [color=darkblue]True[/color]
      .RefreshPeriod = 0
      .WebSelectionType = xlSpecifiedTables
      .WebFormatting = xlWebFormattingNone
      .WebTables = sNumTables
      .WebPre[color=darkblue]For[/color]mattedTextToColumns = [color=darkblue]True[/color]
      .WebConsecutiveDelimitersAsOne = [color=darkblue]True[/color]
      .WebSingleBlockTextImport = [color=darkblue]False[/color]
      .WebDisableDateRecognition = [color=darkblue]False[/color]
      .WebDisableRedirections = [color=darkblue]False[/color]
      .Refresh BackgroundQuery:=[color=darkblue]False[/color]
   [color=darkblue]End[/color] [color=darkblue]With[/color]

   [color=green]'delete the first column[/color]
   Sheets(sSheetName).Columns(1).Delete shift:=xlToLeft
[color=darkblue]End[/color] [color=darkblue]Sub[/color]



[color=darkblue]Sub[/color] SeparateDates([color=darkblue]ByVal[/color] sheetName [color=darkblue]As[/color] [color=darkblue]String[/color])
   [color=darkblue]Dim[/color] lr [color=darkblue]As[/color] [color=darkblue]Long[/color]
   [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
   
   [color=darkblue]With[/color] Sheets(sheetName)
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      [color=darkblue]For[/color] i = lr [color=darkblue]To[/color] 1 [color=darkblue]Step[/color] -1
         
         [color=darkblue]If[/color] i <> 1 [color=darkblue]Then[/color]
            [color=darkblue]If[/color] .Range("A" & i).Value <> .Range("A" & i - 1).Value [color=darkblue]Then[/color]
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
            [color=darkblue]End[/color] [color=darkblue]If[/color]
         [color=darkblue]End[/color] [color=darkblue]If[/color]
      [color=darkblue]Next[/color] i
   [color=darkblue]End[/color] [color=darkblue]With[/color]

[color=darkblue]End[/color] Sub


Sub TidyUp([color=darkblue]ByVal[/color] sheetName [color=darkblue]As[/color] [color=darkblue]String[/color])
   [color=darkblue]Dim[/color] lr [color=darkblue]As[/color] [color=darkblue]Long[/color]
   [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
   [color=darkblue]Dim[/color] txt [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] rw [color=darkblue]As[/color] [color=darkblue]Long[/color]
   
   rw = 1
   [color=darkblue]With[/color] Sheets(sheetName)
      .Range("E1").Value = "Date"
      .Range("F1").Value = "Fixture"
      .Range("G1").Value = "Kick Off"
      
      lr = .Range("A" & Rows.Count).End(xlUp).Row
      For i = 4 [color=darkblue]To[/color] lr
         [color=darkblue]If[/color] .Range("B" & i).Value <> "" [color=darkblue]Then[/color]
            txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
            rw = rw + 1
            .Range("E" & rw).Value = .Range("B" & i).Value
            .Range("F" & rw).Value = txt
            .Range("G" & rw).Value = .Range("C" & i).Text
         [color=darkblue]End[/color] [color=darkblue]If[/color]
      [color=darkblue]Next[/color] i
   [color=darkblue]End[/color] [color=darkblue]With[/color]
   
   [color=darkblue]With[/color] Sheets(sheetName)
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      .Columns("A:D").Delete shift:=xlToLeft
      .Columns("A:C").AutoFit
   [color=darkblue]End[/color] [color=darkblue]With[/color]
   
   [color=green]'copy and paste[/color]
   [color=green]'Sheets(sheetName).UsedRange.Copy _
      Destination:=Sheets("Sheet1").Range("A1")[/color]
[color=darkblue]End[/color] Sub
 
Upvote 0
Thankyou so much for this Bertie, that is simply astounding!

I will be studying this piece for some time in order to understand it, I really appreciate you sparing your time to help.

You are a LEGEND! :biggrin:
 
Upvote 0
Hi Tommy,

Insert a worksheet and name it Template. This sheet will be copied for each league.

Insert a worksheet and name it Data. Lay it out as:
Excel 2007<TABLE style="BORDER-BOTTOM: #a6aab6 1px solid; BORDER-LEFT: #a6aab6 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-COLLAPSE: collapse; BORDER-TOP: #a6aab6 1px solid; BORDER-RIGHT: #a6aab6 1px solid" rules=all cellPadding=2><COLGROUP><COL style="BACKGROUND-COLOR: #e0e0f0" width=25><COL><COL><COL></COLGROUP><THEAD><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #e0e0f0; COLOR: #161120"><TH></TH><TH>A</TH><TH>B</TH><TH>C</TH></TR></THEAD><TBODY><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">1</TD><TD>URL</TD><TD>League/Sheet Name</TD><TD>#Tables</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">2</TD><TD>http://www.bbc.co.uk/sport/football/premier-league/fixtures</TD><TD>EPL</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #ffff00">1,2,3,4</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">3</TD><TD>http://www.bbc.co.uk/sport/football/league-one/fixtures</TD><TD>nPower01</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #ffff00">1,2,3,4</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">4</TD><TD>http://www.bbc.co.uk/sport/football/scottish-premier/fixtures</TD><TD>SPL</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #ffff00">1,2,3</TD></TR></TBODY></TABLE>
Data




NB column C, number of tables on the webpage, may need manual updating.

The code now loops through the Data sheet, copies and renames the Template, and downloads the information from the url.

The Main() procedure controls the code.
Code:
[COLOR=darkblue]Sub[/COLOR] Main()
   [COLOR=darkblue]Dim[/COLOR] rng [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] sUrl [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] sSheetName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] sNumTables [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] rng = Sheets("Data").Range("A2")
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rng = ""
      sUrl = rng.Value
      sSheetName = rng.Offset(, 1).Value
      sNumTables = rng.Offset(, 2).Value
 
      ImportFromWeb sUrl, sSheetName, sNumTables
      TidyUp sSheetName
      SeparateDates sSheetName
 
      [COLOR=darkblue]Set[/COLOR] rng = rng.Offset(1, 0)
   [COLOR=darkblue]Loop[/COLOR]
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Sub[/COLOR] ImportFromWeb([COLOR=darkblue]ByVal[/COLOR] sUrl [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], _
                  [COLOR=darkblue]ByVal[/COLOR] sSheetName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], _
                  [COLOR=darkblue]ByVal[/COLOR] sNumTables [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR])
   [COLOR=green]'copy the template[/COLOR]
   Sheets("Template").Copy After:=Worksheets(Worksheets.Count)
   ActiveSheet.Name = sSheetName
 
   [COLOR=darkblue]With[/COLOR] Sheets(sSheetName).QueryTables.Add(Connection:= _
      "URL;" & sUrl, _
      Destination:=Sheets(sSheetName).Range("$A$1"))
      .Name = "fixtures-data"
      .FieldNames = [COLOR=darkblue]True[/COLOR]
      .RowNumbers = [COLOR=darkblue]False[/COLOR]
      .FillAdjacentFormulas = [COLOR=darkblue]False[/COLOR]
      .PreserveFormatting = [COLOR=darkblue]True[/COLOR]
      .RefreshOnFileOpen = [COLOR=darkblue]False[/COLOR]
      .BackgroundQuery = [COLOR=darkblue]True[/COLOR]
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = [COLOR=darkblue]False[/COLOR]
      .SaveData = [COLOR=darkblue]True[/COLOR]
      .AdjustColumnWidth = [COLOR=darkblue]True[/COLOR]
      .RefreshPeriod = 0
      .WebSelectionType = xlSpecifiedTables
      .WebFormatting = xlWebFormattingNone
      .WebTables = sNumTables
      .WebPre[COLOR=darkblue]For[/COLOR]mattedTextToColumns = [COLOR=darkblue]True[/COLOR]
      .WebConsecutiveDelimitersAsOne = [COLOR=darkblue]True[/COLOR]
      .WebSingleBlockTextImport = [COLOR=darkblue]False[/COLOR]
      .WebDisableDateRecognition = [COLOR=darkblue]False[/COLOR]
      .WebDisableRedirections = [COLOR=darkblue]False[/COLOR]
      .Refresh BackgroundQuery:=[COLOR=darkblue]False[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'delete the first column[/COLOR]
   Sheets(sSheetName).Columns(1).Delete shift:=xlToLeft
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
 
[COLOR=darkblue]Sub[/COLOR] SeparateDates([COLOR=darkblue]ByVal[/COLOR] sheetName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR])
   [COLOR=darkblue]Dim[/COLOR] lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      [COLOR=darkblue]For[/COLOR] i = lr [COLOR=darkblue]To[/COLOR] 1 [COLOR=darkblue]Step[/COLOR] -1
 
         [COLOR=darkblue]If[/COLOR] i <> 1 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] .Range("A" & i).Value <> .Range("A" & i - 1).Value [COLOR=darkblue]Then[/COLOR]
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
               Rows(i).Insert shift:=xlDown
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]Next[/COLOR] i
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
[COLOR=darkblue]End[/COLOR] Sub
 
 
Sub TidyUp([COLOR=darkblue]ByVal[/COLOR] sheetName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR])
   [COLOR=darkblue]Dim[/COLOR] lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] txt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   rw = 1
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      .Range("E1").Value = "Date"
      .Range("F1").Value = "Fixture"
      .Range("G1").Value = "Kick Off"
 
      lr = .Range("A" & Rows.Count).End(xlUp).Row
      For i = 4 [COLOR=darkblue]To[/COLOR] lr
         [COLOR=darkblue]If[/COLOR] .Range("B" & i).Value <> "" [COLOR=darkblue]Then[/COLOR]
            txt = .Range("A" & i).Value & " v " & .Range("A" & i + 2).Value
            rw = rw + 1
            .Range("E" & rw).Value = .Range("B" & i).Value
            .Range("F" & rw).Value = txt
            .Range("G" & rw).Value = .Range("C" & i).Text
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]Next[/COLOR] i
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      lr = .Range("A" & .Rows.Count).End(xlUp).Row
      .Columns("A:D").Delete shift:=xlToLeft
      .Columns("A:C").AutoFit
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'copy and paste[/COLOR]
   [COLOR=green]'Sheets(sheetName).UsedRange.Copy _[/COLOR]
[COLOR=green]     Destination:=Sheets("Sheet1").Range("A1")[/COLOR]
[COLOR=darkblue]End[/COLOR] Sub


Very nice code except I have added code in red to speed up code and msgbox telling time taken.

Code:
Sub Main()
   Dim rng As Range
   Dim sUrl As String
   Dim sSheetName As String
   Dim sNumTables As String
   
   Dim aStartTime
    
   [COLOR=red][B]aStartTime = Now()
   
   'Speeding Up VBA Code
    Application.ScreenUpdating = False 'Prevent screen flickering[/B][/COLOR]
  
   
   Set rng = Sheets("Data").Range("A2")
   Do Until rng = ""
      sUrl = rng.Value
      sSheetName = rng.Offset(, 1).Value
      sNumTables = rng.Offset(, 2).Value
      
      ImportFromWeb sUrl, sSheetName, sNumTables
      TidyUp sSheetName
      SeparateDates sSheetName
      
      Set rng = rng.Offset(1, 0)
   Loop
   
   [COLOR=red][B]'Release memory
    Set rng = Nothing
    
    'Speeding Up VBA Code
    Application.ScreenUpdating = True 'Prevent screen flickering
        
    
    MsgBox "Time taken: " & Format(Now() - aStartTime, "h:mm:ss"), vbInformation, "Job Done"
[/B][/COLOR]End Sub

Biz
 
Upvote 0

Forum statistics

Threads
1,216,176
Messages
6,129,314
Members
449,501
Latest member
Amriddin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top