Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default .find not finding value

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default .find not finding value

I've never been a fan of not specifying all the parms to the .find statement.

..Find(findstring, LookIn:=xlValues, LookAt:=xlWhole)

I'd use:
..Find(what:=findstring, _
after:=.Cells(.cells.count), _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlnext, _
MatchCase:=False)

If you don't specify all the parms, then excel will use whatever was used
last--that includes by your code, someone else's code or even the user.

(Maybe it's the matchcase parm????)

What are you looking for? Sometimes dates can be difficult to get to work
correctly, too.

Emily wrote:

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


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default .find not finding value

thanks Dave. that seems to work. I am just searching for exact match on a
plain string. I will follow your advise when using the .find statement

"Dave Peterson" wrote:

I've never been a fan of not specifying all the parms to the .find statement.

..Find(findstring, LookIn:=xlValues, LookAt:=xlWhole)

I'd use:
..Find(what:=findstring, _
after:=.Cells(.cells.count), _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlnext, _
MatchCase:=False)

If you don't specify all the parms, then excel will use whatever was used
last--that includes by your code, someone else's code or even the user.

(Maybe it's the matchcase parm????)

What are you looking for? Sometimes dates can be difficult to get to work
correctly, too.

Emily wrote:

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


--

Dave Peterson

Reply
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
Find not finding programatically J Streger Excel Programming 1 February 10th 09 07:05 PM
.find not finding corect value treasuresflemar Excel Programming 1 December 11th 07 09:19 PM
Finding row number with .find() treasuresflemar Excel Programming 2 December 10th 07 11:37 PM
Find/Replace not Finding Sjones Excel Discussion (Misc queries) 1 June 21st 07 07:31 PM
How do I get the Excel Find function to keep finding what I want? Clueless Excel Discussion (Misc queries) 3 September 22nd 06 07:09 PM


All times are GMT +1. The time now is 05:36 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"