LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default understanding the .FIND example

Maybe you can set a range equal to the found cells, and then perform the
modifications after everything is found.

Sub Tester1()
Dim FoundRng As Range
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
Set FoundRng = c
firstAddress = c.Address
Do
' c.Value = 5
Set c = .FindNext(c)
Set FoundRng = Union(FoundRng, c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

Dim FoundCell As Range
For Each FoundCell In FoundRng.Cells
FoundCell.Value = 5 ' or whatever
Next

' or in one fell swoop
' FoundRng.Value = 5
End Sub

Find/FindNext gives a granular level of control, but if you need to just
replace 2's with 5's you can use the Replace function

Range("A1:A500").Replace What:=2, Replacement:=5, LookAt:=xlWhole ', etc

One thing..both Find and Replace use the last-used values for unspecified
parameters, so if the macro should find using part of the cell (xlPart), or
the whole cell (xlWhole), specify it in the parameter LookAt. Otherwise, if
someone runs the macro after they manually used Find looking at the Whole
cell (or if another macro did it), that's what the next macro will do too,
unless the macro specifies the LookAt parameter. See the help file for other
parameters, such as MatchCase, etc.


--
Tim Zych
SF, CA

"AlanC" wrote in message
...
I am having difficulty in understanding the .FIND example in the help
files.

With Worksheets(1).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)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

This works fine but in my case I need to do some reordering of rows and
then
continue to FINDNEXT. There appears to be a pointer set within the FIND
method which is where the .Findnext(c) starts. My changes mean that the
pointer is set to a row before the found occurrence and thus the .Findnext
repeats
or
at the end of the range the repeat finds a different address for the
c.address from that stored in firstaddress so loops continuously.

Is the pointer addressable, modificable or am I going about it the wrong
way?

--
AlanC



 
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
Understanding formula Dig Excel Worksheet Functions 1 July 3rd 08 03:31 PM
Not understanding with/end with davegb Excel Programming 7 March 6th 06 09:42 PM
Not understanding If Not..Then nothing davegb Excel Programming 6 June 14th 05 04:49 PM
Understanding Templates Syed Zeeshan Haider Excel Programming 6 May 25th 05 12:39 AM
Understanding declarations Greg[_16_] Excel Programming 1 January 26th 05 09:35 PM


All times are GMT +1. The time now is 02:17 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"