View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dick Kusleika[_3_] Dick Kusleika[_3_] is offline
external usenet poster
 
Posts: 599
Default HowTo Count number of replacements

Keith

I don't think looping through the cells would be that bad. Like this

Sub myrepl2()

Dim rFound As Range
Dim sFirstAdd As String
Dim lRepCnt As Long

Set rFound = Sheet1.Cells.Find("too")

If Not rFound Is Nothing Then
sFirstAdd = rFound.Address

Do
lRepCnt = lRepCnt + 1
Set rFound = Sheet1.Cells.Find("too", rFound)
Loop Until rFound.Address = sFirstAdd
End If

Sheet1.Cells.Replace "too", "two"
MsgBox lRepCnt

End Sub

Another way would be to use the change event, like this in a standard module

Public lReplaceCnt As Long
Public bReplacing As Boolean

Sub myrepl()

lReplaceCnt = 0
bReplacing = True
Sheet1.Cells.Replace "two", "too"
bReplacing = False

MsgBox lReplaceCnt

End Sub

And this in the Sheet's module

Private Sub Worksheet_Change(ByVal Target As Range)

If bReplacing Then
lReplaceCnt = lReplaceCnt + 1
End If

End Sub

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"Keith Harris" wrote in message
...
Hi,

I have a simple macro which will replace one character with another:

ActiveSheet.Cells.Replace "&", "&", xlPart, xlByRows, True

Does anyone know how to return the number of replacements other than
iterating through each cell and counting -- which I imagine would be very
slow.

Thanks for any help.
-Keith