Loop a range in code

ONIWE

New Member
Joined
Jun 29, 2010
Messages
25
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

patel45

Well-known Member
Joined
Jul 15, 2012
Messages
1,953
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

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,943
Office Version
  1. 2013
Platform
  1. Windows
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

ONIWE

New Member
Joined
Jun 29, 2010
Messages
25
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

ONIWE

New Member
Joined
Jun 29, 2010
Messages
25
ADVERTISEMENT
It now works.

Thanks Patel and Daverunt.


Cheers
 
Upvote 0

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,943
Office Version
  1. 2013
Platform
  1. Windows
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

ONIWE

New Member
Joined
Jun 29, 2010
Messages
25
ADVERTISEMENT
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

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,943
Office Version
  1. 2013
Platform
  1. Windows
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

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,943
Office Version
  1. 2013
Platform
  1. Windows
Typical....just as i post.
Great glad it's working though :)
 
Upvote 0

Forum statistics

Threads
1,195,710
Messages
6,011,243
Members
441,596
Latest member
reza_57

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
Top