Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Search always ends in a find, but should not

Can anyone see why the below code results in a MasgBox "Found", even though
there is NO Sheet2.Range("A1").value in Sheet3(SavedData) ?

Sub SaveCoverPage()
' Check to see if the Record Already Exists
Dim rngFound As Range
On Error Resume Next
' Gain the Location of the Cover Page Title
With Worksheets("SavedData").Range("A:A")

Set rngFound = .Find(What:=Sheet2.Range("A1").Value, After:=Cells(1),
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _

SearchDirection:=xlNext,
MatchCase:=True, Matchbyte:=True)
If rngFound.Value < "" Then ' <==== If there is a Sheet2Range("A1").value
found
MsgBox "Found" '
<----------------------------------------------------------- I ALWAYS get
this result, even if there is NO value in the SavedData sheet matching the
valuer in Sheet 2 A1.
Else ' <===== =============== If there is Not Sheet2.Range("A1").value
found
MsgBox "Not Found"
End If
End With
End Sub



Corey....


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default Search always ends in a find, but should not

Hi Corey

Try this for size. This should sort your problem out.

Take care

Marcus

Option Explicit
Sub SaveCoverPage()

Dim rngFound As Range
Dim MyVar As String
On Error Resume Next
MyVar = Sheets(2).Range("A1").Value

With Worksheets("SavedData").Range("A:A")
Set rngFound = Cells.Find(MyVar, LookIn:=xlValues)
If Not rngFound Is Nothing Then
MsgBox rngFound
Else
MsgBox "Not Found"
End If
End With

End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Search always ends in a find, but should not

Thanks Marcus


"marcus" wrote in message
...
Hi Corey

Try this for size. This should sort your problem out.

Take care

Marcus

Option Explicit
Sub SaveCoverPage()

Dim rngFound As Range
Dim MyVar As String
On Error Resume Next
MyVar = Sheets(2).Range("A1").Value

With Worksheets("SavedData").Range("A:A")
Set rngFound = Cells.Find(MyVar, LookIn:=xlValues)
If Not rngFound Is Nothing Then
MsgBox rngFound
Else
MsgBox "Not Found"
End If
End With

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Search always ends in a find, but should not

Sometimes VBA distinguishes between Empty, zero and empty string, so to be
on the safe side, instead of If < "" it might be better to use something
like If "" . That way if the cell has a positive value of any kind, it
will register found, but if it does not have a positive value, it will
register, not found.



"Corey" wrote in message
...
Can anyone see why the below code results in a MasgBox "Found", even
though there is NO Sheet2.Range("A1").value in Sheet3(SavedData) ?

Sub SaveCoverPage()
' Check to see if the Record Already Exists
Dim rngFound As Range
On Error Resume Next
' Gain the Location of the Cover Page Title
With Worksheets("SavedData").Range("A:A")

Set rngFound = .Find(What:=Sheet2.Range("A1").Value, After:=Cells(1),
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _

SearchDirection:=xlNext, MatchCase:=True, Matchbyte:=True)
If rngFound.Value < "" Then ' <==== If there is a Sheet2Range("A1").value
found
MsgBox "Found" '
<----------------------------------------------------------- I ALWAYS get
this result, even if there is NO value in the SavedData sheet matching the
valuer in Sheet 2 A1.
Else ' <===== =============== If there is Not Sheet2.Range("A1").value
found
MsgBox "Not Found"
End If
End With
End Sub



Corey....



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
IF with Search/Find??? TotallyConfused Excel Worksheet Functions 5 August 1st 08 10:05 AM
If Search that Exits Sub or Ends If Ryan Hess Excel Programming 1 July 27th 07 09:16 PM
filter using "ends with" and 3 choices? Ex: ends with 1,2 or3 Debbie Excel Worksheet Functions 1 April 6th 07 12:07 AM
search a string withing a string : find / search hangs itarnak[_9_] Excel Programming 4 October 24th 05 03:19 PM
Can Search find 2 or more "/"? Wind54Surfer Excel Discussion (Misc queries) 2 February 22nd 05 04:31 PM


All times are GMT +1. The time now is 12:35 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"