关于 vba:run time 1004 Error on .Refresh BackgroundQuery

run time 1004 Error on .Refresh BackgroundQuery

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'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


我的解决方案(可能添加格式以将其带回 A 列):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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


这是我到目前为止为每个县声明变量时的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'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