Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
RJH RJH is offline
external usenet poster
 
Posts: 44
Default '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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default '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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default '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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default '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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default '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






  #6   Report Post  
Posted to microsoft.public.excel.programming
RJH RJH is offline
external usenet poster
 
Posts: 44
Default '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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 Macro/VB Question DDE Question MadDog22 Excel Worksheet Functions 1 March 10th 10 01:47 AM
where can I see my question and answer? Yesterday I ask a question IP Excel Discussion (Misc queries) 2 May 10th 08 04:08 PM
question kim010981 Excel Discussion (Misc queries) 1 October 31st 06 12:59 AM
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
The question is an excel question that I need to figure out howto do in excel. Terry Excel Worksheet Functions 3 January 23rd 06 06:22 PM


All times are GMT +1. The time now is 01:08 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"