Find & Replace Problem - ACC97 
Author Message
 Find & Replace Problem - ACC97

This section of code should repeat a search for a string, untill that string is no longer found, but instead it only changes the first occurance.  Rerunning
continues to change the next occurance.  Why won't this loop?

CONTINUESEARCHFORM:
        If Modules("Form_" & Frm.Name).Find(StrOldName, lngSLine, lngSCol, lngELine, lngECol) Then
            strLine = Modules("Form_" & Frm.Name).Lines(lngSLine, Abs(lngELine - lngSLine) + 1)
            intChr = Len(strLine)
            intBefore = lngSCol - 1
            intAfter = intChr - CInt(lngECol - 1)

            strLeft = Left$(strLine, intBefore)
            strRight = Right$(strLine, intAfter)

            ' Make a new string
            strNewLine = strLeft & StrNewName & strRight

            ' Replace original with new string
            Modules("Form_" & Frm.Name).ReplaceLine lngSLine, strNewLine
            If Frm.Name = "ZZ - Replace it Test" Then Stop
            GoTo CONTINUESEARCHFORM
     End If



Tue, 16 Sep 2003 04:39:17 GMT  
 Find & Replace Problem - ACC97
Dan,

You give the End If outside the loop, while the If statement is inside the
loop.

Btw, I would question the use of GoTo in code at any time except with error
handling. A more elegant way to loop
would be something like:

Dim Ok    As Boolean

Ok = False
Do Until Ok = True
        If  <seek string here> Then
                <code>
        Else
                Ok = True
        End If
Loop

Peter



Quote:
> This section of code should repeat a search for a string, untill that

string is no longer found, but instead it only changes the first occurance.
Rerunning
Quote:
> continues to change the next occurance.  Why won't this loop?

> CONTINUESEARCHFORM:
>         If Modules("Form_" & Frm.Name).Find(StrOldName, lngSLine, lngSCol,

lngELine, lngECol) Then
Quote:
>             strLine = Modules("Form_" & Frm.Name).Lines(lngSLine,

Abs(lngELine - lngSLine) + 1)
Quote:
>             intChr = Len(strLine)
>             intBefore = lngSCol - 1
>             intAfter = intChr - CInt(lngECol - 1)

>             strLeft = Left$(strLine, intBefore)
>             strRight = Right$(strLine, intAfter)

>             ' Make a new string
>             strNewLine = strLeft & StrNewName & strRight

>             ' Replace original with new string
>             Modules("Form_" & Frm.Name).ReplaceLine lngSLine, strNewLine
>             If Frm.Name = "ZZ - Replace it Test" Then Stop
>             GoTo CONTINUESEARCHFORM
>      End If



Tue, 16 Sep 2003 05:21:56 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Find& replace one by one

2. Find & Replace using input box

3. FIND & REPLACE

4. Multiple find & replace

5. Find&replace-code

6. find & Replace with wild cards and patterns

7. find/replace on headers&footers

8. Excel macro to find&replace in MSWord

9. Find&Replace Tab

10. Global Find & Replace

11. Find & replace question

12. Advanced Find & Replace

 

 
Powered by phpBB® Forum Software