在.Refresh BackgroundQuery上运行时1004错误

经过一些更新,我已经能够接近工作代码。 我遇到的唯一问题之一就是macros不是从最后一页上抓取数据。 对于昨天的数据,有6页的数据,但macros只是通过第5页。但奇怪的是,如果我从2天前用相同的代码刮取数据,我能够检索所有的数据7或8页。 我不确定这是为什么发生。 有任何想法吗? 这里是更新的代码。

'Macro to query Delinquency Status Search for DFB Counties 'Run Monday to pull data from Friday Sub queryActivityDailyMforFWorking() Dim nextrow As Integer, i As Long Dim dates dates = Date - 1 i = 1 Application.ScreenUpdating = False Application.DisplayStatusBar = True Do 'i = i + 1 Application.StatusBar = "Processing Page " & i nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1 'lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlToLeft).Column + 1 'With ActiveSheet.QueryTables.Add(Connection:= _ '"URL;https://www.myfloridalicense.com/delinquency_results.asp?SID=&page=" & i + county + x & "&status=NS&send_date=" & dates & "&search_1.x=1", _ 'Destination:=Range("A" & nextrow)) With ActiveSheet.QueryTables.Add(Connection:= _ "URL;https://www.myfloridalicense.com/delinquency_results.asp?SID=&page=" & i & "&county_1=11,%2012,%2013,%2014,%2015,%2016,%2017,%2018,%2019,%2020,%2021,%2022,%2023,%2024,%2025,%2026,%2027,%2028,%2080,%2029,%2030,%2031,%2032,%2033,%2034,%2035,%2036,%2037,%2038,%2039,%2040,%2041,%2042,%2043,%2044,%2045,%2046,%2047,%2048,%2049,%2050,%2051,%2052,%2053,%2054,%2055,%2056,%2057,%2058,%2059,%2079,%2060,%2061,%2062,%2063,%2064,%2067,%2068,%2069,%2065,%2066,%2070,%2071,%2072,%2073,%2078,%2074,%2075,%2076,%2077&status=NS&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 ActiveCell.value = ActiveCell.Value * 2 ActiveCell.Offset(1,0).Select Loop Until IsEmpty(ActiveCell.Value) 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 End Sub 

我的解决scheme(也许添加格式,使其回到列A):

 Sub QueryDelinquencyTest() Dim nextrow As Integer, i As Integer Dim dates dates = Date - 1 Application.ScreenUpdating = False Do While i < 25 'this is the page range to be captured. 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=AL&status=NS&send_date=" & dates & "&search_1.x=1", _ Destination:=Range("A" & nextrow)) .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "10" .WebPreFormattedTextToColumns = False .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With i = i + 1 Loop Cells.Select With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With End Sub 

这是我为每个县申报variables时的代码。

 'Macro to query Delinquency Status Search for DFB Counties 'Run Monday to pull data from Friday Sub queryActivityDailyMforF() Dim nextrow As Integer, i As Long Dim dates dates = Date - 1 Dim x, county1, county2, county3, county4, county5, county6, county7, county8, county9, county10, county11, county12 county1 = "county_1=16" county2 = "county_1=21" county3 = "county_1=23" county4 = "county_1=32" county5 = "county_1=36" county6 = "county_1=41" county7 = "county_1=46" county8 = "county_1=53" county9 = "county_1=54" county10 = "county_1=57" county11 = "county_1=60" county12 = "county_1=66" 'Dim myString 'myString = "No Activity Information Found" 'Dim lastRow As Long 'Dim county 'Dim site As String 'Dim rng As Range 'Dim firstCell As String 'lastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row Application.ScreenUpdating = False Application.DisplayStatusBar = True 'If Not rng Is Nothing Then firstCell = rng.Address 'Do Until myString <> lastRow And InStr("&county_1=66", "St. Lucie") Do 'Do While i < 4 'For i = 1 To lastRow 'Set rng = Sheets("sheet2").Range("A:A").find(What:=Cells(i, 1), LookIn:=xlValues, lookAt:=xlPart, SearchOrder:=xlByRows) 'Do While lastRow <> myString 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=NS&send_date=" & dates & "&search_1.x=1", _ ' Destination:=Range("A" & nextrow)) With ActiveSheet.QueryTables.Add(Connection:= _ "URL;https://www.myfloridalicense.com/delinquency_results.asp?SID=&page=" & i & county & x & "&status=NS&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 'If Not rng Is Nothing Then ' If rng.Address = firstCell Then Exit Do ' End If 'site = "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=NS&send_date=" & dates & "&search_1.x=1" 'county = "&coutny_1=66" End With 'Next i = i + 1 Loop Until x = 12 x = x + 1 'Loop Until InStr(site, county) And ActiveCell.Value = myString 'Wend 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 'Next 'Loop End Sub