E
Emily
I have the following code to find and replace a values within a range that
works fine most of the time but under some situtations which I have not
determined it only seems to look at the current cell. If I select the range
first it does work.
I have set the breakpoint prior to this code being called and done a find
through the interface with out selecting a range. It doesn't the find the
value as it only seems to be searching the current cell. In order to get
find in the interface working to search the sheet again again I have to
select a range first.
Do I need to set some other parameter or clear something to make sure it is
using my range not the current cell or is my only option to include selection
of the range first in the code?
Public Function replaceStringInRange(r As range, findstring As String,
replacewith As String) As Integer
' finds cells within range replaces string
Dim firstaddress As String
Dim c As Range
Dim count As Integer
count = 0
With r
Set c = .Find(findstring, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
firstaddress = c.Address
Do
c.Value = replacewith
count = count + 1
Set c = .FindNext(c)
If c Is Nothing Then
Exit Do
End If
If c.Address = firstaddress Then
Exit Do
End If
Loop
End If
End With
replaceStringInRange = count
End Function
works fine most of the time but under some situtations which I have not
determined it only seems to look at the current cell. If I select the range
first it does work.
I have set the breakpoint prior to this code being called and done a find
through the interface with out selecting a range. It doesn't the find the
value as it only seems to be searching the current cell. In order to get
find in the interface working to search the sheet again again I have to
select a range first.
Do I need to set some other parameter or clear something to make sure it is
using my range not the current cell or is my only option to include selection
of the range first in the code?
Public Function replaceStringInRange(r As range, findstring As String,
replacewith As String) As Integer
' finds cells within range replaces string
Dim firstaddress As String
Dim c As Range
Dim count As Integer
count = 0
With r
Set c = .Find(findstring, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
firstaddress = c.Address
Do
c.Value = replacewith
count = count + 1
Set c = .FindNext(c)
If c Is Nothing Then
Exit Do
End If
If c.Address = firstaddress Then
Exit Do
End If
Loop
End If
End With
replaceStringInRange = count
End Function