problem with .FindNext

B

Bob Cochran

I'm in need of some help here, and also with a macro question, too.
I need to replace text

From To
A1 A01
The series goes from A1-A32, B1-B32, ...R32 that are on many sheets

I have no problem changing the text, but can't seem to get the .FindNext to
work.
I get Compile error: Invalid or unqualified reference
on line
Set Myfound = .FindNext(Myfound)

Here is my macro (minus some code to shorten the message)
Can someone Please Help Me! Thanks in advance

Sub replaceinworkbook()
'
' replaceinworkbook Macro
' Macro recorded 10/8/2003 by rcochran
'
Dim strPin As String 'holds string to search i.e.
A1,A2...A9,B1,B2...B9,...R9
Dim n As Integer ' variable for ASCII characters Chr(n)
Dim m As Integer ' variable for incrementing 1 to 9
Dim Myfound As Range ' cell containing the text you want to search
Dim cell As String 'variable to hold found cell text
Dim firstAddress As String 'variable to keep a reference to the first
address of the Myfound
Dim n_len As Integer 'variable to count number of text characters in
Myfound
Dim m_instr As Integer 'variable count where strPin is located in Myfound


For n = 65 To 82
For m = 1 To 9
strPin = Chr(n) & m 'initialize variable to A1. Chr(65) is A, so
Chr(65) & 1 is A1

' set Myfound to the cells that are found during search
Set Myfound = Cells.Find(What:=strPin, after:=ActiveCell,
LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:=False)

If Myfound Is Nothing Then 'if strPin is not found leave If

GoTo exit_if 'jump out of If

Else

Myfound.Activate 'if strPin is found, then activate cell
firstAddress = Myfound.address

Do

cell = ActiveCell.FormulaR1C1 'set cell to Myfound
n_len = Len(cell) ' return number of characters in
cell

code removed from this message

Set Myfound = .FindNext(Myfound)
Loop While Not Myfound Is Nothing And Myfound.address <>
firstAddress

end if

exit_if:

Next m
Next n

End Sub
 
B

Bob Cochran

I've already looked at the example many times and that is
what brought me to seek the help from the MVP's. Sorry if
this is not challenging enough.
 
C

Chip Pearson

Bob,

Change
Set Myfound = .FindNext(Myfound)
To
Set Myfound = Cells.FindNext(Myfound)
 
B

bob cochran

Don,

I must first say that I owe you an apology for replying so sarcasticly.
Now I would like to thank you for making me think. I was finally able
to get the code to work. It was a little more difficult than I
explained. When the line firstAddress = Myfound.address was executed,
the findnext never returned to firstAddress.

For those who may look for examples in the future, I was trying to
change "A1" to "A01". The problem was that some cells contained just
"A1", some "Pin A1", and others "Block One A1". The code would work up
until it found a cell containing "A10". I did not want it to change it
to "A010", so the code would continue looping through all cells
containing "A10". I created a new String variable nextAddress to equal
Myfound.address only if m_instr = 0 (which used InStr to search cells
for "A1" & " "), and used a loop until m_instr>0 or nextAddress <>
Myfound.address

Sometimes it is better to learn from being a little frustrated.

Thanks again,
Bob
 
B

bob cochran

Chip,

Thanks, at one point I did change to Cells.FindNext, then later I
removed and was able to get it to work. Was it something about the way
my variables were named? I was not sure what should be 'range', and
what should be set to 'string'. Now I've got it.. "I hope"!

Can you give me any tips on making the code run quicker? I have many
workbooks, each with 3 sheets with range of A1:E1500 to run this on.
Guess it is definitely quicker than doing it by hand.

By the way, I have found your website very beneficial.

Thanks,
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top