【问题标题】:Importing data from multiple pages of a single website从单个网站的多个页面导入数据
【发布时间】: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


【解决方案1】:

你已经很接近了。

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

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"

【讨论】:

  • 非常感谢伙计。感谢你的帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 2021-04-06
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
相关资源
最近更新 更多