ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   why can't 'Do While Each Cell in Range...'? (https://www.excelbanter.com/excel-programming/283111-why-cant-do-while-each-cell-range.html)

Ian Elliott[_3_]

why can't 'Do While Each Cell in Range...'?
 
Thanks in advance.
I have some code that finds the first non-zero cell in a
row:
Dim cell as Range
RowNumber=1
For Each cell In Range("D1:" & ActiveSheet.Range
("D65536").End(xlUp).Address)
If cell = 0 then
RowNumber = RowNumber + 1 'increase by one row
Else
Exit For
End If
Next
But I figure I could get this down a line or two by:
Do While Each cell in Range("D1:" & ActiveSheet.Range
("D65536").End(xlUp).Address) < 0
RowNumber=RowNumber+1
Loop
But I get a syntax error (the line goes red) when I type
in the above Do While...
Is the 'each' code useable in Do Whiles?
If not, is there something else I can do?
Thanks again!

Bernie Deitrick[_2_]

why can't 'Do While Each Cell in Range...'?
 
Ian,

You can't use that structure, but you could use:

RowNumber = Application.CountIf(Range("D1",
Range("D65536").End(xlUp)), 0) +1

HTH,
Bernie
MS Excel MVP

"Ian Elliott" wrote in message
...
Thanks in advance.
I have some code that finds the first non-zero cell in a
row:
Dim cell as Range
RowNumber=1
For Each cell In Range("D1:" & ActiveSheet.Range
("D65536").End(xlUp).Address)
If cell = 0 then
RowNumber = RowNumber + 1 'increase by one row
Else
Exit For
End If
Next
But I figure I could get this down a line or two by:
Do While Each cell in Range("D1:" & ActiveSheet.Range
("D65536").End(xlUp).Address) < 0
RowNumber=RowNumber+1
Loop
But I get a syntax error (the line goes red) when I type
in the above Do While...
Is the 'each' code useable in Do Whiles?
If not, is there something else I can do?
Thanks again!





All times are GMT +1. The time now is 11:22 AM.

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