Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default A question relating to "for each cell_in_loop in range"

I realize this is a somewhat more advance question while at the
same time somewhat simple. At the same time my problem has to
do with a rather bizarre.. well, let me describe the situation.

Given
ExitLine = 2000
For each cell_in_loop In Range("H2:H2000")
If (cell_in_loop.Offset(0, -7).Value = "EOList") Then
' ExitLine = ' ?? <------- This is where the problem arises
Exit For
End If
: Lengthy list of instructions
Next
MsgBox "Exited after line " & ExitLine

As you can see, this is, quite simply put, a straight forward,
"for...next" loop with no real "gotcha's" beyond a more-or-less
straight forward "get out early" clause which is also quite
straight forward.

I can know, with a fair degree of certainty that if the "loop
runs to the end" I have 2000 (or more) lines of data. Here's
where I've run into the problem. I haven't been able to find
a way to find out where I leave the loop, when I leave the
loop, early. I know there must be a way and I'm sure it's
quite simple, but for the life of me it's escaping me.

I've tried toying around with the internals of the "offset"
using "application.row()" but the compiler complains and
when I can get it to a run-able state it blows up as soon
as it hits that line.

I've tried counters but that got too convoluted to be
cleanly implemented (since the columns might have to
be sorted from time-to-time) and "EOList" may move to
a different place. (And the code was *butt crack ugly"
and too hard to hard to read as well. The understanding
or comprehension required a brain-damaged chimp with a
Ph.D in some obscure language no one has spoken for ten
million years. I'd like to leave code that the next guy
could "fix," if he had too, without boiling me in VR oil
each time he had to do a "fix.")

I tried some C++ routines but it was too easy to get lost
between the "external calls" to be certain I was really
certain that what I was getting was a correct value. (I
mean it "looked right" but it "looked wrong" too.)

So, I'll entertain all suggestions on how I can find out that
when I find "EOList" that the last line of usable data was:
"Row() - 1?"

Looking forward to good ideas.


David

(Newsgroups have *never* failed me in the past and I the idea
of them doing so now is "inconceivable." I can't even picture
the idea of them doing so now. - With slight credit to "The
Brute" in "The Princess Bride")




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default A question relating to "for each cell_in_loop in range"

ExitLine = 2000
For each cell_in_loop In Range("H2:H2000")
If (cell_in_loop.Offset(0, -7).Value = "EOList") Then
ExitLine = Cells_In_Loop.Row - 1
Exit For
End If
: Lengthy list of instructions
Next
MsgBox "Exited after line " & ExitLine

would be my guess.

--
Regards,
Tom Ogilvy


"David F. Schrader" wrote in message
...
I realize this is a somewhat more advance question while at the
same time somewhat simple. At the same time my problem has to
do with a rather bizarre.. well, let me describe the situation.

Given
ExitLine = 2000
For each cell_in_loop In Range("H2:H2000")
If (cell_in_loop.Offset(0, -7).Value = "EOList") Then
' ExitLine = ' ?? <------- This is where the problem arises
Exit For
End If
: Lengthy list of instructions
Next
MsgBox "Exited after line " & ExitLine

As you can see, this is, quite simply put, a straight forward,
"for...next" loop with no real "gotcha's" beyond a more-or-less
straight forward "get out early" clause which is also quite
straight forward.

I can know, with a fair degree of certainty that if the "loop
runs to the end" I have 2000 (or more) lines of data. Here's
where I've run into the problem. I haven't been able to find
a way to find out where I leave the loop, when I leave the
loop, early. I know there must be a way and I'm sure it's
quite simple, but for the life of me it's escaping me.

I've tried toying around with the internals of the "offset"
using "application.row()" but the compiler complains and
when I can get it to a run-able state it blows up as soon
as it hits that line.

I've tried counters but that got too convoluted to be
cleanly implemented (since the columns might have to
be sorted from time-to-time) and "EOList" may move to
a different place. (And the code was *butt crack ugly"
and too hard to hard to read as well. The understanding
or comprehension required a brain-damaged chimp with a
Ph.D in some obscure language no one has spoken for ten
million years. I'd like to leave code that the next guy
could "fix," if he had too, without boiling me in VR oil
each time he had to do a "fix.")

I tried some C++ routines but it was too easy to get lost
between the "external calls" to be certain I was really
certain that what I was getting was a correct value. (I
mean it "looked right" but it "looked wrong" too.)

So, I'll entertain all suggestions on how I can find out that
when I find "EOList" that the last line of usable data was:
"Row() - 1?"

Looking forward to good ideas.


David

(Newsgroups have *never* failed me in the past and I the idea
of them doing so now is "inconceivable." I can't even picture
the idea of them doing so now. - With slight credit to "The
Brute" in "The Princess Bride")






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default A question relating to "for each cell_in_loop in range"

Hi
This is one way:

Public Sub tester()
Dim MyRange As Range, cell_in_loop As Range
Dim FirstLine As Long, ExitLine As Long
Dim ExitLineFound As Boolean
Set MyRange = ActiveSheet.Range("H2:H2000")
FirstLine = MyRange.Row
ExitLineFound = False
For Each cell_in_loop In MyRange
If (cell_in_loop.Offset(0, -7).Value = "EOList") Then
ExitLine = cell_in_loop.Row - FirstLine + 1
ExitLineFound = True
Exit For
End If
Next cell_in_loop
If ExitLineFound Then
MsgBox "Exited at line " & ExitLine
Else
MsgBox "The text EOList was not found"
End If
Set MyRange = Nothing
End Sub

The Row property of a Range object gives its first row number (the
numbers you see on the Excel sheet down the left hand side)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default A question relating to "for each cell_in_loop in range"

I was certain I answered this at the individual
level but just to be safe...

Paul this works as you indicated. It's a little
out of the way of what I wanted but I've
worked it into my code so that now I've
got what I need.

David

wrote in message
oups.com...
Hi
This is one way:

Public Sub tester()
Dim MyRange As Range, cell_in_loop As Range
Dim FirstLine As Long, ExitLine As Long
Dim ExitLineFound As Boolean
Set MyRange = ActiveSheet.Range("H2:H2000")
FirstLine = MyRange.Row
ExitLineFound = False
For Each cell_in_loop In MyRange
If (cell_in_loop.Offset(0, -7).Value = "EOList") Then
ExitLine = cell_in_loop.Row - FirstLine + 1
ExitLineFound = True
Exit For
End If
Next cell_in_loop
If ExitLineFound Then
MsgBox "Exited at line " & ExitLine
Else
MsgBox "The text EOList was not found"
End If
Set MyRange = Nothing
End Sub

The Row property of a Range object gives its first row number (the
numbers you see on the Excel sheet down the left hand side)



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 - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
"Disk is Full" add-on question to "Can't reset last cell" post tod [email protected] Excel Discussion (Misc queries) 0 January 22nd 07 02:32 AM
Using "Cells" to write "Range("A:A,H:H").Select" Trip Ives[_2_] Excel Programming 3 June 5th 04 03:13 PM
Question about sorting and "key" range values TBA[_2_] Excel Programming 3 September 25th 03 02:01 PM


All times are GMT +1. The time now is 10:38 AM.

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

About Us

"It's about Microsoft Excel"