1

I am new to VBA and have been facing a lot of trouble with figuring out how to pull data from the website boxofficemojo.com. I am trying to extract weekly data for the years 2010-2015. So i found a code that did something along the same lines and changed it to suit my needs. It is as follows

Sub Movies()
Dim nextRow As Integer, i As Integer
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
For i = 1 To 52 
Application.StatusBar = "Processing Page " & i
nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.boxofficemojo.com/weekly/chart/?yr=2015&wk=&p=.htm" & i, _
Destination:=Range("A" & nextRow))

.Name = "weekly/chart/?yr=2015&wk=&p=.htm"
.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 = xlWebFormattingAll
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ThisWorkbook.Save
Next i
Application.StatusBar = False
End Sub

However instead of pulling the data for weeks 1 - 52 of 2015, it keeps on pulling data for the latest week of 2016 and repeats it 52 times. I am not sure what's wrong here and any help would be really really appreciated.

Thanks for your effort.

4

1 回答 1

1

你离那里很近。

url将方法中的字符串调整为QueryTables.Add

http://www.boxofficemojo.com/weekly/chart/?yr=2015&wk=" & i & "&p=.htm"

既然您说您想要 2010 年到 2015 年。您可以将现有循环包装在另一个循环中For x = 2010 to 2015,然后将 URL 修改为:

http://www.boxofficemojo.com/weekly/chart/?yr=" & x & "&wk=" & i & "&p=.htm"
于 2016-07-24T20:45:42.373 回答