Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Stopping Find from wrapping around in code?

I'm using Find to loop through column ET on my sheet and find all the
"2"s. I stop the search when the .Row of the last .FindNext is past
the last row of the sheet.

But it never is. That's because when it actually does find the last
one, the next .FindNext wraps back to the top of the column again!

Is there some way to stop it? The Help doesn't seem to suggest a
solution.

Maury
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Stopping Find from wrapping around in code?

You need to store where you found the first instance and then stop looping
when you get back to there... Code similar to this should do the trick...

Sub FindStuff()
Dim rngToSearch As Range
Dim rngFound As Range
Dim rngFoundall As Range
Dim strFirstAddress As String

Set rngToSearch = Sheets("Sheet1").Range("A1:A100")
Set rngFound = rngToSearch.Find(What:="this", _
LookAt:=xlWhole, _
LookIn:=xlFormulas, _
MatchCase:=False)
If rngFound Is Nothing Then
MsgBox "Sorry... Not Found"
Else
strFirstAddress = rngFound.Address
Set rngFoundall = rngFound
Do
Set rngFoundall = Union(rngFound, rngFoundall)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
rngFoundall.EntireRow.Select
End If
End Sub
--
HTH...

Jim Thomlinson


"Maury Markowitz" wrote:

I'm using Find to loop through column ET on my sheet and find all the
"2"s. I stop the search when the .Row of the last .FindNext is past
the last row of the sheet.

But it never is. That's because when it actually does find the last
one, the next .FindNext wraps back to the top of the column again!

Is there some way to stop it? The Help doesn't seem to suggest a
solution.

Maury

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Stopping Find from wrapping around in code?

Ok, fair enough, but here's another...

Am I right in thinking that you cannot find in hidden columns?
Whenever I try to do a ast.Range("ET:ET").Find... after hiding the
columns, it fails and tells me the With block is not set.

Maury
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Stopping Find from wrapping around in code?

Post your full code...
--
HTH...

Jim Thomlinson


"Maury Markowitz" wrote:

Ok, fair enough, but here's another...

Am I right in thinking that you cannot find in hidden columns?
Whenever I try to do a ast.Range("ET:ET").Find... after hiding the
columns, it fails and tells me the With block is not set.

Maury

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Stopping Find from wrapping around in code?

In Help search for "Find Method", then look at the example

Regards,
Peter T

"Maury Markowitz" wrote in message
...
I'm using Find to loop through column ET on my sheet and find all the
"2"s. I stop the search when the .Row of the last .FindNext is past
the last row of the sheet.

But it never is. That's because when it actually does find the last
one, the next .FindNext wraps back to the top of the column again!

Is there some way to stop it? The Help doesn't seem to suggest a
solution.

Maury





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Stopping Find from wrapping around in code?

On Jun 18, 3:32*pm, "Peter T" <peter_t@discussions wrote:
In Help search for "Find Method", then look at the example


Umm, I specifically mentioned that I looked in the Help example.

Maury
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Stopping Find from wrapping around in code?

You didn't specifically mention you looked in the help example. This is what
you said -
"The Help doesn't seem to suggest a solution."
which I took to mean for some reason you were unable to find the example
which, as far as I can see, answers your original question. In case you
didn't find it here it is -

Find Method Example

This example finds all cells in the range A1:A500 on worksheet one that
contain the value 2, and then it makes those cells gray.

With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.Pattern = xlPatternGray50
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

Regards,
Peter T


"Maury Markowitz" wrote in message
...
On Jun 18, 3:32 pm, "Peter T" <peter_t@discussions wrote:
In Help search for "Find Method", then look at the example


Umm, I specifically mentioned that I looked in the Help example.

Maury


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Stopping Find from wrapping around in code?


someone correct me if I am wrong but I believe there is an error with the
code construct of the Find Method help file example (2003)? even if not, I
did have problems with the example and now use modified version as follows.

Dim c As Range
Dim FirstAddress As String
With Worksheets(2).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
If c Is Nothing Then Exit Do
Loop Until c.Address = FirstAddress
End If
End With


hope of some interest.

--
jb


"Peter T" wrote:

In Help search for "Find Method", then look at the example

Regards,
Peter T

"Maury Markowitz" wrote in message
...
I'm using Find to loop through column ET on my sheet and find all the
"2"s. I stop the search when the .Row of the last .FindNext is past
the last row of the sheet.

But it never is. That's because when it actually does find the last
one, the next .FindNext wraps back to the top of the column again!

Is there some way to stop it? The Help doesn't seem to suggest a
solution.

Maury




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Stopping Find from wrapping around in code?

I tried your example exactly as posted with some 2's in column A on the
second sheet. It worked fine for me (though it wouldn't if the column was
hidden)

Regards,
Peter T

"john" wrote in message
...

someone correct me if I am wrong but I believe there is an error with the
code construct of the Find Method help file example (2003)? even if not, I
did have problems with the example and now use modified version as

follows.

Dim c As Range
Dim FirstAddress As String
With Worksheets(2).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
If c Is Nothing Then Exit Do
Loop Until c.Address = FirstAddress
End If
End With


hope of some interest.

--
jb


"Peter T" wrote:

In Help search for "Find Method", then look at the example

Regards,
Peter T

"Maury Markowitz" wrote in message

...
I'm using Find to loop through column ET on my sheet and find all the
"2"s. I stop the search when the .Row of the last .FindNext is past
the last row of the sheet.

But it never is. That's because when it actually does find the last
one, the next .FindNext wraps back to the top of the column again!

Is there some way to stop it? The Help doesn't seem to suggest a
solution.

Maury






  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Stopping Find from wrapping around in code?

Hi Peter,
thanks for feedback & good point about hidden columns.
--
jb


"Peter T" wrote:

I tried your example exactly as posted with some 2's in column A on the
second sheet. It worked fine for me (though it wouldn't if the column was
hidden)

Regards,
Peter T

"john" wrote in message
...

someone correct me if I am wrong but I believe there is an error with the
code construct of the Find Method help file example (2003)? even if not, I
did have problems with the example and now use modified version as

follows.

Dim c As Range
Dim FirstAddress As String
With Worksheets(2).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
If c Is Nothing Then Exit Do
Loop Until c.Address = FirstAddress
End If
End With


hope of some interest.

--
jb


"Peter T" wrote:

In Help search for "Find Method", then look at the example

Regards,
Peter T

"Maury Markowitz" wrote in message

...
I'm using Find to loop through column ET on my sheet and find all the
"2"s. I stop the search when the .Row of the last .FindNext is past
the last row of the sheet.

But it never is. That's because when it actually does find the last
one, the next .FindNext wraps back to the top of the column again!

Is there some way to stop it? The Help doesn't seem to suggest a
solution.

Maury








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
stopping code Jase Excel Discussion (Misc queries) 1 October 6th 08 05:42 PM
VBA code stopping in odd places JDub Setting up and Configuration of Excel 2 October 10th 06 08:04 AM
Stopping errors when a find statement doesn't find! matpj[_34_] Excel Programming 3 January 25th 06 02:23 PM
Stopping Code Nigel Excel Programming 2 January 20th 06 04:39 PM
stopping code from looping tjb Excel Worksheet Functions 3 December 7th 05 02:02 AM


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

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"