Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For each... Problem 2

Status
Not open for further replies.

Igwiz

Technical User
Jun 3, 2003
88
0
0
CA
Hi,

Got a problem with a For Each loop

This is my code:

Sub D()

Worksheets("DG").Select

For Each cell In Worksheets("CDS").Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Cells

Unfortunately it bombs out at the For Each line. I have tried different alternatives and it appears to fall over as soon as I put in the worksheets("") part of the for each range reference. Can you not call a for each loop reference from a different sheet to the active one?

Many thanks,

Ig
 
try typing in the full spreadsheet name with .xls extension. then try it with network location if you are running it from a folder that is different to the on with the excel sheets.
 
You can, but in this way:
[tt]With Worksheets("CDS")
For Each cell In .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown)).Cells
....
Next cell
End With[/tt]

combo

 
Hi Ig,

The problem is that the Cells property, without qualification, belongs to the Application object and refers to cells on the Active Sheet which is "DG" (because you have Selected it). To explicitly refer to cells on worksheet "CDS" you must qualify the references to Cells; you do not, however, need to qualify the Range itself as you are defining it explicitly, and also the .Cells on the end does nothing at all so may as well not be there, and you end up with ..

Code:
For Each cell In Range(Worksheets("CDS").Cells(1, 1), Worksheets("CDS").Cells(1, 1).End(xlDown))

Enjoy,
Tony
 
Great guys. Used yours Tony. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top