Loop also taking blank cells when there is just one row

fari1

Active Member
Joined
May 29, 2011
Messages
362
Hi,
I've a loop code, which runs through a column and connects every URL in that column to the web and brings in data. it works perfect, when there'r two or more than two cells of URLs, but when there's just one cell of URL, then, it starts considering the blank cell as well into the loop and obviously gives error. can somebody amend it to make it work even when there is single cell of data



Code:
Sub loopB()
Dim cel As Range
Dim mySheet As Worksheet
Set mySheet = Sheets("sheet1")
For Each cel In Sheets("URLs").Range("A2:A" & Sheets("URLs").Range("A2").End(xlDown).Row)
mySheet.Range("A1") = cel.Value
Next cel
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This is because End(xlDown) skips to the bottom when its starting point is the first and only entry in the list.

Try starting it from Range("A1") instead.
 
Upvote 0
still it dun work, can u pl give me the code, that may skip blank cells when it moves down
 
Upvote 0
Is the list unbroken or does it have random gaps in it? If it has gaps, you'll need a whole lot more code.

If the list is unbroken then my solution should work, as long as A1 something in it (e.g. a column header)
 
Upvote 0
the list is not broken, it has no gaps,and also in my other loop the data is starting from A1, and still there it is giving error
 
Upvote 0
Is this what you tried?

Code:
Sub loopB()

Dim cel As Range
Dim mySheet As Worksheet

Set mySheet = Sheets("sheet1")
For Each cel In Sheets("URLs").Range("A2:A" & Sheets("URLs").Range("A1").End(xlDown).Row)
    mySheet.Range("A1") = cel.Value
Next cel

End Sub

When you get an error, what does the error say? What part of the code gets highlighted by the error?
 
Upvote 0
how can i start the next string with A1, while earlier in the code it has A2. it didn't work
 
Upvote 0
Because EndDown will return A2, unless you have no data in the list at all. Can you upload your spreadsheet?
 
Upvote 0
here is got the code, problem resolved, thanks anyways:)

Code:
Sub loopB()
Dim cel As Range
Dim mySheet As Worksheet
Set mySheet = Sheets("sheet1")
For Each cel In Sheets("URLs").Range("A2:A" & Sheets("URLs").Range("A2").End(xlDown).Row)
mySheet.Range("A1") = cel.Value
Next cel
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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