Loop a range in code

ONIWE

New Member
Joined
Jun 29, 2010
Messages
27
Hello,

Please how can I put a loop in the code below to change the range so the code can perform 10 or more different searches. that is change range("y5") to Y6 and more.

With Sheets("Creditsafe").QueryTables.Add(Connection:="URL;" & Sheets("report").range("y5").Value, _
Destination:=Sheets("Creditsafe").range("A1"))
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
set rng=Sheets("report").Range("Y5:Y25")
for each cell in rng
   With Sheets("Creditsafe").QueryTables.Add(Connection:="URL;" & cell.Value, _
         Destination:=Sheets("Creditsafe").range("A1"))
.........
next
 
Upvote 0
Hi,

something along the lines of:

Code:
Inpt = InputBox("Enter end search number")
For x = 5 To Inpt

With Sheets("Creditsafe").QueryTables.Add(Connection:="URL;" & Sheets("report").Range("y" & x).Value, _
Destination:=Sheets("Creditsafe").range("A1"))'Destination:=Sheets("Creditsafe").Range("A1"))
End With

Next
 
Upvote 0
Guys,
thanks but Im struggling with it. Its still returning errors with both codes. that keeps bouncing back as it cannot find the right URL.

.Refresh BackgroundQuery:=False
 
Upvote 0
Hi,

I was just about to respond.
that failure appears on the specified line if there is no valid URL in one of the search cells.

Glad it's working.
 
Upvote 0
Hi Dave,

One more problem. Please see below.

Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=False, Transpose:=False
Sheets("creditsafe").Delete

Next

Application.ScreenUpdating = False
Loop Until IsEmpty(Sheets("report").Range("y" & x))
End Sub




How do I end the code. It returns errors once it gets to a blank and i tried using a loop to stop it.
 
Upvote 0
Hi,

Not sure how it's going to fit into your current code.
I am posting the code i used for testing. Hopefully you'll be able to see what you need.


Code:
Sub URL_Get_Query()
x = 1
For x = 1 To 25
Do While Not IsEmpty(Sheets("Sheet3").Range("A" & x).Value)
  With ActiveSheet.QueryTables.Add(Connection:="URL;" & Sheets("Sheet3").Range("A" & x).Value, _
       Destination:=Sheets("Sheet2").Range("A1")) 'Destination:=Sheets("Creditsafe").Range("A1"))
      .BackgroundQuery = True
      .TablesOnlyFromHTML = True
      .Refresh BackgroundQuery:=False
      .SaveData = True
   End With
   x = x + 1
 Loop
   Next
End Sub
 
Upvote 0
Typical....just as i post.
Great glad it's working though :)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top