ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   'For - Next' question (https://www.excelbanter.com/excel-programming/321558-next-question.html)

RJH

'For - Next' question
 
Why wouldn't this statement bail out after 150 rows?

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row Step 1

I use a 'If RowNdx 150 then exit sub' in my loop to bail out when needed,
but I'm sure there's a better way.

Thanks for the help.

RJH



Colo[_105_]

'For - Next' question
 
Hi RJD,

Why you don't simply use For RowNdx = 1 to 150?
I think we need to know the whole code inside of the For Next looping.


--
Regards,
Colo
http://www.puremis.net/excel/


"RJH" wrote in message
...
Why wouldn't this statement bail out after 150 rows?

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row Step

1

I use a 'If RowNdx 150 then exit sub' in my loop to bail out when

needed,
but I'm sure there's a better way.

Thanks for the help.

RJH




Rob van Gelder[_4_]

'For - Next' question
 
Looks fine to me...

This outputs 1 to 150 as expected.

Sub test()
Dim RowNdx As Long

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row
Step 1
Debug.Print RowNdx
Next
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"RJH" wrote in message
...
Why wouldn't this statement bail out after 150 rows?

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row Step
1

I use a 'If RowNdx 150 then exit sub' in my loop to bail out when
needed, but I'm sure there's a better way.

Thanks for the help.

RJH




Bob Phillips[_6_]

'For - Next' question
 
If you have to specify the start and end rows as arguments to the Cells
property, then it would seem to me that you might just as well say

For RowNdx = 1 To 150

there is no more dynamic aspect to your code.

Which leads on to, post the rest of the code.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"RJH" wrote in message
...
Why wouldn't this statement bail out after 150 rows?

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row Step

1

I use a 'If RowNdx 150 then exit sub' in my loop to bail out when

needed,
but I'm sure there's a better way.

Thanks for the help.

RJH





Tom Ogilvy

'For - Next' question
 
And one more possibility

Dim rng as Range, cell as Range
Set rng = Sheets(1).Range(Sheets(1).Cells(1, 3), _
Sheets(1).Cells(150, 3))

for each cell in rng
msgbox cell.Address

Next


or

Dim rng as Range, cell as Range
set rng = Sheets(1).Cells(1,3).Resize(150,1)
for each cell in rng
msgbox cell.Address
Next

--
Regards,
Tom Ogilvy


"RJH" wrote in message
...
Why wouldn't this statement bail out after 150 rows?

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row Step

1

I use a 'If RowNdx 150 then exit sub' in my loop to bail out when

needed,
but I'm sure there's a better way.

Thanks for the help.

RJH





RJH

'For - Next' question
 
Your comments made me look closely at what I was trying to do.
I have a Do-Loop nested in the For-Next and the Do-Loop was still involved
at row 150.
That's why it wouldn't bail out.

Thanks for your help.

RJH



"RJH" wrote in message
...
Why wouldn't this statement bail out after 150 rows?

For RowNdx = Sheets(1).Cells(1, 3).Row To Sheets(1).Cells(150, 3).Row Step
1

I use a 'If RowNdx 150 then exit sub' in my loop to bail out when
needed, but I'm sure there's a better way.

Thanks for the help.

RJH





All times are GMT +1. The time now is 06:55 PM.

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