我在使用Excel进行网页报废时遇到问题。我想要这个网站的数据:charitynavigator.org
我正在对此链接列表进行测试:http://www.charitynavigator.org/index.cfm?bay=search.results&cgid=7&cuid=30
我通过第一个链接(The Aims Project)记录了我的一个宏。然后,我添加了一个"循环" (?)有人在YouTube视频中使用过。视频制作者解释了代码的逻辑,因此循环似乎有意义并适用于我的问题。但是,在抓取第一页后,该程序无法进入下一个网页。
这是我的代码
Sub adds()
'For i = 1 to 5
For x = 1 To 5
Worksheets("IntlHum").Select
Worksheets("IntlHum").Activate
mystr = "URL;http://www.charitynavigator.org/index.cfm?bay=search.summary&orgid=3803"
mystr = Cells(x, 1)
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = x
With ActiveSheet.QueryTables.Add(Connection:= _
mystr, _
Destination:=Range("$A$1"))
'.CommandType = 0
.Name = "index.cfm?bay=search.summary&orgid=3803_1"
.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 = "2,3,4,5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Next x
End Sub
YouTube视频https://www.youtube.com/watch?v=qbOdUaf4yfI 我是一个完整的新手。我理解逻辑,曾与STATA和LaTexx合作,但我不是计算机科学家。请保持行话最小化。谢谢!
答案 0 :(得分:1)
在第二遍(x = 2
)中," ActiveSheet"可能已更改,因此行mystr = Cells(x, 1)
将不会返回预期值,因为它可能会从您想要的不同数据源读取。 (有关详细信息,请参阅Office documentation of Application.Cells Property (Excel))。 " ActiveSheet"的变化是作为调用Office documentation of Worksheets.Add Method (Excel)
将其更改为mystr = Worksheets("IntlHum").Cells(x, 1)
可能有所帮助。
但我的答案是学习如何使用Excel VBA调试,包括"步入(F8)","即时窗口打印东西"。
这是任何严肃的Excel VBA必须学习的工具,这是用于对代码进行故障排除的工具。
Excel VBA(以及一般的Visual Basic)并非为计算机科学家设计(或预期),而是针对大众"所以你应该很容易找到自己的方式(在学习了一些必要的"行话")之后。
您应该能够使用Google找到许多符合您当前技能的教程资源。作为一种此类资源,您可以尝试http://www.excel-easy.com/vba/examples/debugging.html