【发布时间】:2016-07-24 20:17:15
【问题描述】:
我是 VBA 新手,在弄清楚如何从网站 boxofficemojo.com 提取数据时遇到了很多麻烦。我正在尝试提取 2010-2015 年的每周数据。因此,我找到了一个执行相同操作的代码,并对其进行了更改以满足我的需要。如下
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
但是,它不会提取 2015 年第 1 至第 52 周的数据,而是继续提取 2016 年最后一周的数据并重复 52 次。我不确定这里出了什么问题,非常感谢任何帮助。
感谢您的努力。
【问题讨论】:
-
如果您使用 Excel 2010 或更高版本,使用 Power Query 可能会更容易
标签: excel import excel-web-query vba