ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For Each Next Loop (https://www.excelbanter.com/excel-programming/309810-re-each-next-loop.html)

Harald Staff

For Each Next Loop
 
What does this return ?

j = Cells(Rows.Count, 4).End(xlUp)(2).Row
MsgBox j
Set rng1 = Range(Cells(2, 4), Cells(j, 4))
MsgBox rng1.Address

Best wishes Harald

"benb" skrev i melding
...
I am using the following code:

j = Cells(Rows.Count, 4).End(xlUp)(2).Row
Set rng1 = Range(Cells(2, 4), Cells(j, 4)
For Each cll In rng1
If Not cll.Value Like "NY*" Then
cll.EntireRow.Clear
End If
Next cll

When executed, this clears the contents of row 1 even though the range I
want to work with is row 2 and down. I could use a different loop to
accomplish the same thing, but I am really interested in understanding why
the code is executing on elements outside of the specified group. Am I

using
it incorrectly or do I just not understand how this code works? Thanks.




Harald Staff

For Each Next Loop
 
Cool. Thanks for the feedback.

Best wishes Harald

"benb" skrev i melding
...
Thanks Harald. That helped me figure out the source of my problem. The
range was shifting because some rows were deleted after I set the range.

I
just set the range later in the code and it works fine. Thanks again.





All times are GMT +1. The time now is 12:03 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com