【发布时间】:2016-01-12 02:21:50
【问题描述】:
我正在创建一个宏来从网站上抓取数据。我遇到的问题是当最后一个完整页面被抓取并且 A 列没有数据时,但其他列有,我收到运行时 1004 错误。例如,如果要抓取的总页数为 6,并且 A 列没有关于第 5 页的最后一个条目的数据,则宏将抓取第 5 页的所有数据,但在尝试获取时会抛出运行时错误到第6页。第6页也有数据,但我在想,由于A列中没有数据,它只是决定给出运行时错误。对此有什么想法吗?另外,使用我所包含的代码,在下一个箭头消失之前有宏循环会更容易吗?如果是这样,我会怎么做?
'Macro to query Daily Activity Search for DFB Counties
'Run Monday to pull data from Friday
Sub queryActivityDailyMforFWorking()
Dim nextrow As Integer, i As Integer
Dim dates
dates = Date - 3
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
Do While i <= 50
Application.StatusBar = "Processing Page " & i
nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).row + 1
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://www.myfloridalicense.com/delinquency_results.asp?SID=&page=" & i & "&county_1=16&county_1=21&county_1=23&county_1=32&county_1=36&county_1=41&county_1=46&county_1=53&county_1=54&county_1=57&county_1=60&county_1=66&status=R&send_date=" & dates & "&search_1.x=1", _
Destination:=Range("A" & nextrow))
'.Name = _
"2015&search_1.x=40&search_1.y=11&date=on&county_1=AL&lic_num_del=&lic_num_rep=&status=NS&biz_name=&owner_name="
.FieldNames = False
.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 = "10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
'autofit columns
Columns("A:G").Select
Selection.EntireColumn.AutoFit
'check for filter, if not then turn on filter
ActiveSheet.AutoFilterMode = False
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A:G").AutoFilter
End If
i = i + 1
End With
Application.StatusBar = False
'Align text left
Cells.Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Loop
End Sub
【问题讨论】:
-
当我选择调试时,它显示错误即将发生
.Refresh BackgroundQuery :=False -
是否设置为后台查询为真,但刷新为假?
-
是的,我相信...
-
有零页吗?您声明 i 但从不为其赋值,因此在第一个循环中 i 为 0。
-
@Jeeped 不,没有零页。我忘了将该部分包含在代码中。即使我声明 i 的值为 1,我仍然收到相同的错误消息
标签: vba excel web-scraping