4
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.

1
  • what does .Refresh BackgroundQuery even do? Commented Apr 5, 2011 at 23:54

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

keep in mind the code uses urls supplied in a spreadsheet so if you want the code to work you need to input your own i am using "flvtubesearch.co/…" as the url and "?tmp=toolbar_FlvTube_homepage&prt=flvtubetb04ie&clid=acd050b75c114e5ba9a270e770c9cf6f" as the path
also changing the .refresh backgroundquery: = False to .refresh = false fails and gives the error "object doesn't support this method"
On another note if you change the for next loop to for i=2 to 2 the code runs just find, so the problem is somehow related to for...next loops
sorry I meant that just use .Refresh
do you realise that you are putting the data into the same range each time. so you will end up with 48 querytables all pointing to the same spot which is probably where the problem is
|
1

The only way to resolve this issue is to delete the active query table after each iteration. A useful example solution provides:

https://social.technet.microsoft.com/Forums/office/en-US/956dc1b6-bd37-4b97-a042-ba2a37f729b6/removing-querytables-and-leaving-the-results?forum=excel

1 Comment

The resource cannot be found.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.