Refresh backgroundquery false ошибка

Sub Macro1() Dim URL As String Dim Path As String Dim i As Integer For i = 2 To 50 If Range("Prices!E" & i).Value 1 Then URL = Range("Prices!D" & i).Text Path = Range("Prices!F" &a...
Sub Macro1()
Dim URL As String
Dim Path As String
Dim i As Integer
For i = 2 To 50
If Range("Prices!E" & i).Value <> 1 Then
URL = Range("Prices!D" & i).Text
Path = Range("Prices!F" & i).Text
End If
Sheet19.Activate
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & URL _
    , Destination:=ActiveSheet.Range("$A$1"))
    .Name = _
    "" & Path
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    //'In the Line above the above
    //'Run time error '1004
    //'An unexpected error has occured
End With
Next i
End Sub

The code above creates an error at the specified line. A google search on .Refresh BackgroundQuery shows that it is picky in its functionality in loops. Simply deleting the line makes nothing show up in excel.

With the current error message the code works fine for the first i value and then breaks.

For Answer and comments-
TLDR: .Refresh BackgroundQuery:=False will fail if your query input is invalid or malformed. The problem in this case was the for…next loop was calling cells to use as url’s that hand no values in them. However it will fail anytime the query is malformed.

asked Apr 5, 2011 at 23:10

iwishiwasacodemonkey's user avatar

1

All the previous lines inside the With statement are setting properties.
the .Refresh BackgroundQuery := False is a method call.

The refresh is supposed to refresh the results.
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh

Query Table Refresh Method Help Link

Edit
It would appear that there is something wrong with the URL and when it goes to refresh it is unable to do it. could be a proxy issue, or not connected to the network, or the URL does not exist.

answered Apr 6, 2011 at 1:38

Nathan Fisher's user avatar

Nathan FisherNathan Fisher

7,8913 gold badges49 silver badges67 bronze badges

9

R. Oosterholt's user avatar

answered Aug 1, 2011 at 12:21

a fascinated excel user's user avatar

1

I’m not sure why my fix worked, but here it is:

I also used querytables.add within a for loop, and I was adding .asc files. This error was only popping up after the last addition—so my program essentially did what I wanted it to, but it would interrupt function. On the last run through the For loop, I removed the .Refresh BackgroundQuery:=False statement. It was necessary for it to paste my data for all the previous runs through the For loop.

Basically I replaced this:

          .Refresh BackgroundQuery:=False

With this:

          If Index = ctr Then

          Else
               .Refresh BackgroundQuery:=False
          End If

answered Jun 10, 2016 at 13:57

Shizinator's user avatar

Sub Macro1()
Dim URL As String
Dim Path As String
Dim i As Integer
For i = 2 To 50
If Range("Prices!E" & i).Value <> 1 Then
URL = Range("Prices!D" & i).Text
Path = Range("Prices!F" & i).Text
End If
Sheet19.Activate
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & URL _
    , Destination:=ActiveSheet.Range("$A$1"))
    .Name = _
    "" & Path
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    //'In the Line above the above
    //'Run time error '1004
    //'An unexpected error has occured
End With
Next i
End Sub

The code above creates an error at the specified line. A google search on .Refresh BackgroundQuery shows that it is picky in its functionality in loops. Simply deleting the line makes nothing show up in excel.

With the current error message the code works fine for the first i value and then breaks.

For Answer and comments-
TLDR: .Refresh BackgroundQuery:=False will fail if your query input is invalid or malformed. The problem in this case was the for…next loop was calling cells to use as url’s that hand no values in them. However it will fail anytime the query is malformed.

asked Apr 5, 2011 at 23:10

iwishiwasacodemonkey's user avatar

1

All the previous lines inside the With statement are setting properties.
the .Refresh BackgroundQuery := False is a method call.

The refresh is supposed to refresh the results.
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh

Query Table Refresh Method Help Link

Edit
It would appear that there is something wrong with the URL and when it goes to refresh it is unable to do it. could be a proxy issue, or not connected to the network, or the URL does not exist.

answered Apr 6, 2011 at 1:38

Nathan Fisher's user avatar

Nathan FisherNathan Fisher

7,8913 gold badges49 silver badges67 bronze badges

9

R. Oosterholt's user avatar

answered Aug 1, 2011 at 12:21

a fascinated excel user's user avatar

1

I’m not sure why my fix worked, but here it is:

I also used querytables.add within a for loop, and I was adding .asc files. This error was only popping up after the last addition—so my program essentially did what I wanted it to, but it would interrupt function. On the last run through the For loop, I removed the .Refresh BackgroundQuery:=False statement. It was necessary for it to paste my data for all the previous runs through the For loop.

Basically I replaced this:

          .Refresh BackgroundQuery:=False

With this:

          If Index = ctr Then

          Else
               .Refresh BackgroundQuery:=False
          End If

answered Jun 10, 2016 at 13:57

Shizinator's user avatar

  • Remove From My Forums
  • Question

  • This might be a common problem and I found another topic that relates to mine but it isn’t quiet the same and I can’t find a solution to my problem by using it’s solution.

    I have a VBA script for getting data from a HTML table to a Excel file. At the HTML page the VBA script first logges in with a name and password written in the Excel file and given with the GET method.

    The HTML checks if the user is already logged in (session variables) and if so it gets the data from the table to Excel. If not the user is logged in and then the data is send.

    When I run the script it returns a 1004 error. This error is given at «.Refresh BackgroundQuery:=False».

    From using Google I understand that a 1004 error is given when the browser returns an errorcode that VBA doesn’t know.

    When I open the page (In both cases (with the username and password in the URL and already logged in without this)) in any browser (Safari, Firefox, Internet Explorer) the page doesn’t return any error nor report any problems with the page.

    My code:

    Sub GetData() 'Private Sub Workbook_Open() Private Sub Worksheet_Activate()<br/>
      Dim usr As String    'Username<br/>
      Dim pwd As String    'Password<br/>
      Dim url As String    'URL with username and password --> GET<br/>
      Dim baseUrl As String  'URL of page<br/>
      Dim connUrl As String  'URL for connection to HTML<br/>
      <br/>
      usr = ActiveSheet.Range("A1").Value<br/>
      pwd = ActiveSheet.Range("B1").Value<br/>
      <br/>
      baseUrl = "http://xxx/xx/xx/xx/xx/xx/xx.aspx"<br/>
      url = baseUrl & "?usr=" & usr & "&pwd=" & pwd<br/>
      connUrl = "URL;" & baseUrl<br/>
    <br/>
      'open Internet Explorer<br/>
      Set ie = CreateObject("InternetExplorer.Application")<br/>
    <br/>
      'go to .Navigate and show this<br/>
      With ie<br/>
        .Visible = True<br/>
        .Navigate url<br/>
    <br/>
        'do this until the page is fully loaded<br/>
        Do While ie.busy And Not ie.readystate = 4<br/>
          DoEvents<br/>
        Loop<br/>
        <br/>
        'create a Web Query in Sheet1 with connURL beginning from cell A5<br/>
        With ActiveSheet.QueryTables.Add(Connection:=connUrl, Destination:=Range("A4"))<br/>
          .Name = "stockListQuery"<br/>
          .RowNumbers = False<br/>
          .FillAdjacentFormulas = False<br/>
          .PreserveFormatting = True<br/>
          .RefreshOnFileOpen = False<br/>
          .BackgroundQuery = False<br/>
          .RefreshStyle = xlOverwriteCells<br/>
          .SavePassword = False<br/>
          .SaveData = True<br/>
          .AdjustColumnWidth = True<br/>
          .RefreshPeriod = 0<br/>
          .WebSelectionType = xlAllTables<br/>
          .WebFormatting = xlWebFormattingNone<br/>
          .WebPreFormattedTextToColumns = True<br/>
          .WebConsecutiveDelimitersAsOne = True<br/>
          .WebSingleBlockTextImport = False<br/>
          .WebDisableDateRecognition = False<br/>
          .WebDisableRedirections = False<br/>
          <br/>
          .Refresh BackgroundQuery:=False<br/>
          <br/>
        <br/>
        End With<br/>
        Do While ie.busy And Not ie.readystate = 4<br/>
          DoEvents<br/>
        Loop<br/>
        <br/>
      End With<br/>
      <br/>
      'close Internet Explorer<br/>
      ie.Quit<br/>
      <br/>
      Set ie = Nothing<br/>
    End Sub<br/>
    
    

Answers

  • Three suggestions

    1. Maybe you don’t have an active worksheet that you can write to.  Place the macro into a new workbook and see if you get the same error.  The worksheet may be protected.
    2. Try changing the URL to a different web page and see if you get an error
    3. Record a new macro and go to the same webpage and see if you get an error.

    jdweng

    • Marked as answer by

      Thursday, December 30, 2010 12:55 PM

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Sub formir_balance()
  Application.ScreenUpdating = False
  Dim i, r, j
  
'GoTo test
  
    Sheets("import").QueryTables(1).Refresh BackgroundQuery:=False      \Вот здесь выдает ошибку
    Sheets("obrab").Range("L11:V65536").FillDown
    Sheets("svod_tab").PivotTables("СводнаяТаблица1").PivotCache.Refresh
    
    Sheets("Лист1").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
    
    Sheets("Лист1").Select
    Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;\MAR-ckanalitikрисканалитикаIMPORT_Для баланса mas_verTest.txt", _
        Destination:=Range("A1"))
        .Name = "Test_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1251
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileFixedColumnWidths = Array(33, 23, 14, 16, 17, 19, 15, 15, 8, 10)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Columns("A:A").ColumnWidth = 18.43
    Columns("A:A").ColumnWidth = 22
    
    
     Sheets("Лист1").Select
    Range("D4:F4").Select
    Selection.Copy
    Sheets("bal_n").Select
    Range("AL1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    With Selection.Font
        .Name = "Times New Roman"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Selection.Font.Bold = True
    Columns("AM:AM").EntireColumn.AutoFit
  
  
  
  'удаление старых данных
     Sheets("Изм счета").Select
    Range("C3:H300").Select
    Selection.ClearContents
    
    'Sheets("Лист1").Select
    'Cells.Select
    'Selection.Delete Shift:=xlUp
 '   Sheets("bal_n").Select
    
    
' удаление пустых строк
 '   LastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
 ' Application.ScreenUpdating = False
  'For r = LastRow To 1 Step -1
 ' If Application.CountA(Rows(r)) = 0 Then Rows(r).Delete
'  Next r
  
  
'     i = 2
'    j = 2
 
'   форматируем колонку Z - формат Общий
'   ThisWorkbook.Sheets("Лист1").Columns("Z:Z").NumberFormat = "General"
    
'    While ThisWorkbook.Sheets("Лист1").Cells(i, 2) <> ""
        'заполнение колонки Z кодом
        'ThisWorkbook.Sheets("Лист1").Cells(i, 26) = "=RC[-24]&RC[-19]"
        
'    While Range("B10:K7000").Select
'    Selection.Range ("B9:B7000")
'    Range("B9:B7000").Select
'test:
    For j = 3 To ThisWorkbook.Sheets("Изм счета").UsedRange.Rows.Count
        'If ThisWorkbook.Sheets("Изм счета").Cells(j, 2) <> "" Then
            For i = 10 To ThisWorkbook.Sheets("Лист1").UsedRange.Rows.Count
                If ThisWorkbook.Sheets("Лист1").Cells(i, 2) = ThisWorkbook.Sheets("Изм счета").Cells(j, 2) Then
                'присваиваем новое значение кода на листе "Лист1"
                    ThisWorkbook.Sheets("Изм счета").Cells(j, 3) = ThisWorkbook.Sheets("Лист1").Cells(i, 3)
                    ThisWorkbook.Sheets("Изм счета").Cells(j, 4) = ThisWorkbook.Sheets("Лист1").Cells(i, 4)
                    ThisWorkbook.Sheets("Изм счета").Cells(j, 5) = ThisWorkbook.Sheets("Лист1").Cells(i, 5)
                    ThisWorkbook.Sheets("Изм счета").Cells(j, 6) = ThisWorkbook.Sheets("Лист1").Cells(i, 6)
                    ThisWorkbook.Sheets("Изм счета").Cells(j, 7) = ThisWorkbook.Sheets("Лист1").Cells(i, 7)
                    ThisWorkbook.Sheets("Изм счета").Cells(j, 8) = ThisWorkbook.Sheets("Лист1").Cells(i, 8)
'               ThisWorkbook.Sheets("Лист1").Cells(i, 6) = ThisWorkbook.Sheets("Изм данные").Cells(j, 5)
                'Range("i,3:i,8").Select
                'Selection.Copy
                'Sheets("Изм счета").Select
                'Range("j,3:j,8").Select
                'ActiveSheet.Paste
                    Exit For
                End If
            Next
       ' End If
    Next
'        i = i + 1
'    Wend
    Sheets("Изм счета").Select
    Columns("C:H").Select
    Range("C2").Activate
    Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        Range("J1").Select
    Selection.Copy
    Sheets("Изм счета").Select
    Range("C3:H300").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlMultiply, _
    SkipBlanks:=False, Transpose:=False
    Sheets("bal_n").Select
    MsgBox ("  Отчет сформирован!")
    
      
    
End Sub
Sub Macro1()
Dim URL As String
Dim Path As String
Dim i As Integer
For i = 2 To 50
If Range("Prices!E" & i).Value <> 1 Then
URL = Range("Prices!D" & i).Text
Path = Range("Prices!F" & i).Text
End If
Sheet19.Activate
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & URL _
    , Destination:=ActiveSheet.Range("$A$1"))
    .Name = _
    "" & Path
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    //'In the Line above the above
    //'Run time error '1004
    //'An unexpected error has occured
End With
Next i
End Sub

The code above creates an error at the specified line. A google search on .Refresh BackgroundQuery shows that it is picky in its functionality in loops. Simply deleting the line makes nothing show up in excel.

With the current error message the code works fine for the first i value and then breaks.

For Answer and comments-
TLDR: .Refresh BackgroundQuery:=False will fail if your query input is invalid or malformed. The problem in this case was the for…next loop was calling cells to use as url’s that hand no values in them. However it will fail anytime the query is malformed.

asked Apr 5, 2011 at 23:10

iwishiwasacodemonkey's user avatar

1

All the previous lines inside the With statement are setting properties.
the .Refresh BackgroundQuery := False is a method call.

The refresh is supposed to refresh the results.
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh

Query Table Refresh Method Help Link

Edit
It would appear that there is something wrong with the URL and when it goes to refresh it is unable to do it. could be a proxy issue, or not connected to the network, or the URL does not exist.

answered Apr 6, 2011 at 1:38

Nathan Fisher's user avatar

Nathan FisherNathan Fisher

7,8913 gold badges49 silver badges67 bronze badges

9

R. Oosterholt's user avatar

answered Aug 1, 2011 at 12:21

a fascinated excel user's user avatar

1

I’m not sure why my fix worked, but here it is:

I also used querytables.add within a for loop, and I was adding .asc files. This error was only popping up after the last addition—so my program essentially did what I wanted it to, but it would interrupt function. On the last run through the For loop, I removed the .Refresh BackgroundQuery:=False statement. It was necessary for it to paste my data for all the previous runs through the For loop.

Basically I replaced this:

          .Refresh BackgroundQuery:=False

With this:

          If Index = ctr Then

          Else
               .Refresh BackgroundQuery:=False
          End If

answered Jun 10, 2016 at 13:57

Shizinator's user avatar

Sub Macro1()
Dim URL As String
Dim Path As String
Dim i As Integer
For i = 2 To 50
If Range("Prices!E" & i).Value <> 1 Then
URL = Range("Prices!D" & i).Text
Path = Range("Prices!F" & i).Text
End If
Sheet19.Activate
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & URL _
    , Destination:=ActiveSheet.Range("$A$1"))
    .Name = _
    "" & Path
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    //'In the Line above the above
    //'Run time error '1004
    //'An unexpected error has occured
End With
Next i
End Sub

The code above creates an error at the specified line. A google search on .Refresh BackgroundQuery shows that it is picky in its functionality in loops. Simply deleting the line makes nothing show up in excel.

With the current error message the code works fine for the first i value and then breaks.

For Answer and comments-
TLDR: .Refresh BackgroundQuery:=False will fail if your query input is invalid or malformed. The problem in this case was the for…next loop was calling cells to use as url’s that hand no values in them. However it will fail anytime the query is malformed.

asked Apr 5, 2011 at 23:10

iwishiwasacodemonkey's user avatar

1

All the previous lines inside the With statement are setting properties.
the .Refresh BackgroundQuery := False is a method call.

The refresh is supposed to refresh the results.
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh

Query Table Refresh Method Help Link

Edit
It would appear that there is something wrong with the URL and when it goes to refresh it is unable to do it. could be a proxy issue, or not connected to the network, or the URL does not exist.

answered Apr 6, 2011 at 1:38

Nathan Fisher's user avatar

Nathan FisherNathan Fisher

7,8913 gold badges49 silver badges67 bronze badges

9

R. Oosterholt's user avatar

answered Aug 1, 2011 at 12:21

a fascinated excel user's user avatar

1

I’m not sure why my fix worked, but here it is:

I also used querytables.add within a for loop, and I was adding .asc files. This error was only popping up after the last addition—so my program essentially did what I wanted it to, but it would interrupt function. On the last run through the For loop, I removed the .Refresh BackgroundQuery:=False statement. It was necessary for it to paste my data for all the previous runs through the For loop.

Basically I replaced this:

          .Refresh BackgroundQuery:=False

With this:

          If Index = ctr Then

          Else
               .Refresh BackgroundQuery:=False
          End If

answered Jun 10, 2016 at 13:57

Shizinator's user avatar

Понравилась статья? Поделить с друзьями:
  • Renault ошибка u3003 16
  • Refmon initialization failed windows 10 как исправить ошибку
  • Renault ошибка p0638
  • Renault ошибка df1012
  • Renault ошибка df061