Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 441
Default "FIND" generates "Type mismatch" error

I have the following code, but it generates a type mismatch error. Also, I
need to allow for the event nothing is found. Can someone please show me how
to correct this?

Dim lngRow as Long
lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row

Thanks much in advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default "FIND" generates "Type mismatch" error

1st be aware that you are searching in col 23 of the used range which
may/may not be col 23 of the sheet. Suggest just columns(23) if that is the
column you want. Add an error trapper as shown.

Dim lngRow as Long
on error goto notfound

lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row

exit sub
notfound:
msgbox "not found"



--
Don Guillett
SalesAid Software

"quartz" wrote in message
...
I have the following code, but it generates a type mismatch error. Also, I
need to allow for the event nothing is found. Can someone please show me

how
to correct this?

Dim lngRow as Long
lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row

Thanks much in advance.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default "FIND" generates "Type mismatch" error

You've probably alluded to the problem -- if the target isn't found, the
result of Find is Nothing, and Nothing doesn't have a row number. Try
splitting it up:

Dim lngRow as Long
Dim FoundCell As Range

Set FoundCell = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False)

If FoundCell Is Nothing Then
'??
Else
lngRow = FoundCell.Row
End If

or leave as-is and use error trapping:

Dim lngRow as Long
On Error Resume Next
lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row
If Err.Number < 0 Then ....

On Tue, 16 Nov 2004 05:14:04 -0800, quartz
wrote:

I have the following code, but it generates a type mismatch error. Also, I
need to allow for the event nothing is found. Can someone please show me how
to correct this?

Dim lngRow as Long
lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row

Thanks much in advance.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default "FIND" generates "Type mismatch" error

Hi Quartz,

You need a bit of error trapping code in your routine to accommodate this
eventuality.

If there is an error, immediately after the find-code add an if-line:

If lngRow = 0 then
OR
If Err then

And deal with the error. In my example I have just added a message box and
exit the routine.




Sub FindMe()
Dim Rng as Range
Dim lngRow as Long

On Error Resume Next
Set Rng = ActiveSheet.UsedRange.Columns(23)
With Rng
lngRow = .Find(What:="A", After:=.Cells(1), LookIn:=xlFormulas,
LookAt:=xlWhole, _
SearchOrder:=xlByRows,SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Row
End With
If lngRow = 0 Then
MsgBox "Nothing found!"
Exit Sub
Else
Rem Otherwise put whatever you want to do here.
End If
On Error GoTo 0

End Sub

Regards
Paul

"quartz" wrote in message
...
I have the following code, but it generates a type mismatch error. Also, I
need to allow for the event nothing is found. Can someone please show me
how
to correct this?

Dim lngRow as Long
lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row

Thanks much in advance.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 441
Default "FIND" generates "Type mismatch" error

Thanks very much to all who replied. I got it working.

"quartz" wrote:

I have the following code, but it generates a type mismatch error. Also, I
need to allow for the event nothing is found. Can someone please show me how
to correct this?

Dim lngRow as Long
lngRow = ActiveSheet.UsedRange.Columns(23).Find(What:="A", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Row

Thanks much in advance.



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
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
"Type mismatch" when I try to fill an Array variable with "+" [email protected] Excel Discussion (Misc queries) 1 April 17th 07 01:28 PM
HELP on "left","right","find","len","substitute" functions serene83 Excel Discussion (Misc queries) 5 June 27th 06 02:23 AM
Copying data to another worksheet gives "Type Mismatch" error TB[_3_] Excel Programming 6 July 28th 03 12:44 PM


All times are GMT +1. The time now is 06:56 PM.

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"