Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Why does a find wrongly return true when I search a variable?

Hi,

Can anyone help with me a problem I am having with the find function
in VBA? The following code seems to work...

On Error Resume Next

X = Cells.Find(What:="Name", After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate

MsgBox (X)

If "Name" is present, this will return true, if name is not present,
the message box is blank.

This is fine, but I need to loop through this statement as I am trying
to automate the process of todying up data from an unformatted txt
file to another worksheet. There are not always the same amount of
names in the sheets so it needs to be able to keep searching until
there are no more matches. So my idea was this (obviously I want to do
more than just display a message box when I find the cell, but you get
the idea...)

On Error Resume Next

Dim i
i = 1

Do

X = Cells.Find(What:="Name" & i, After:=ActiveCell,
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate

MsgBox (X)

i = i +1

Loop Until x < "True"

It now seems to continuously return true, even when the value does not
appear on the page. Maybe I am going about this all the wrong way, but
has anyone come up against this sort of need for a loop, and could you
help me with why this won't work, or indeed advise of another way of
what I need to do. If you need me to be any more specific, just let me
know, but I think this covers it.

Cheers
KK
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Why does a find wrongly return true when I search a variable?


Review the example code in Help for the Find method.
Note the use of the Set statement and the FindNext method...
'--
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.Pattern = xlPatternGray50
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With
'--

Also, avoid the use of On Error Resume Next.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins - "Find and List" - find multiple items on multiple sheets)




"StraightEight"
wrote in message
Hi,
Can anyone help with me a problem I am having with the find function
in VBA? The following code seems to work...

On Error Resume Next
X = Cells.Find(What:="Name", After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
MsgBox (X)

If "Name" is present, this will return true, if name is not present,
the message box is blank.
This is fine, but I need to loop through this statement as I am trying
to automate the process of todying up data from an unformatted txt
file to another worksheet. There are not always the same amount of
names in the sheets so it needs to be able to keep searching until
there are no more matches. So my idea was this (obviously I want to do
more than just display a message box when I find the cell, but you get
the idea...)
On Error Resume Next

Dim i
i = 1
Do

X = Cells.Find(What:="Name" & i, After:=ActiveCell,
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
MsgBox (X)
i = i +1
Loop Until x < "True"

It now seems to continuously return true, even when the value does not
appear on the page. Maybe I am going about this all the wrong way, but
has anyone come up against this sort of need for a loop, and could you
help me with why this won't work, or indeed advise of another way of
what I need to do. If you need me to be any more specific, just let me
know, but I think this covers it.
Cheers
KK
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Why does a find wrongly return true when I search a variable?

My apologies for the delayed response, this was indeed (after a bit of
tweaking, headaches and frustration!) the correct method for the task
I was needing to do. Many thanks for pointing me where I should have
looked first!

Regards,
Str8

On 4 Feb, 13:37, "Jim Cone" wrote:
Review the example code in Help for the Find method.
Note the use of the Set statement and the FindNext method...
'--
With Worksheets(1).Range("a1:a500")
* * Set c = .Find(2, lookin:=xlValues)
* * If Not c Is Nothing Then
* * * * firstAddress = c.Address
* * * * Do
* * * * * * c.Interior.Pattern = xlPatternGray50
* * * * * * Set c = .FindNext(c)
* * * * Loop While Not c Is Nothing And c.Address < firstAddress
* * End If
End With
'--

Also, avoid the use of On Error Resume Next.
--
Jim Cone
San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins - "Find and List" - find multiple items on multiple sheets)

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
Search an alphanumeric in a column and return true/false 2226 Excel Worksheet Functions 9 January 6th 09 09:17 AM
Search for 2 true arguments and return true or false David Excel Discussion (Misc queries) 3 July 15th 06 10:18 AM
Search column for value and return TRUE or FALSE Remote Desktop Connection hotkey Excel Worksheet Functions 8 July 13th 06 05:07 PM
Return blank cell if 'find' statement not true Kanga 85 Excel Worksheet Functions 4 May 28th 06 04:25 PM
Function to return True/False if all are validated as True by ISNU Tetsuya Oguma Excel Worksheet Functions 2 March 15th 06 10:28 AM


All times are GMT +1. The time now is 12:15 AM.

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

About Us

"It's about Microsoft Excel"