Why does this worksheet event - Error: Type Mismatch ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim Item As String
Dim NItem As String
Dim rFound As Range

Lresult = Len(Target)

'Avoid the endless loop:
Application.EnableEvents = False

Item = Target.Value

If Lresult = 10 Then
    With Sheets("Sheet2")
        Set rFound = .Columns(1).Find(What:=Item, After:=.Cells(1, 1) _
                , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows _
                , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
        On Error GoTo 0
    
    End With
MsgBox (Item)
   
ElseIf Lresult = 15 Then
NItem = Val(Left(Item, 13))
        With Sheets("Sheet2")
        Set rFound = .Columns(1).Find(What:=NItem, After:=.Cells(1, 2) _
                , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows _
                , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
        On Error GoTo 0

    End With
MsgBox (NItem)
    
Else
    
End If

'Enable the Events again:
Application.EnableEvents = True

End Sub

The error occurs at: Find(What:=NItem

I'm guessing due to the left function but can't figure out a solution

Anyone see anything wrong?

Basically wanting to use the first 10 characters in lookup when the Len is 15

Appreciate any help
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
i'm wondering if
NItem = Val(Left(Item, 13)) needs to have VAL as it has been Dimmed as STRING
 
Upvote 0
i'm wondering if
NItem = Val(Left(Item, 13)) needs to have VAL as it has been Dimmed as STRING

Yes this is what I tried but it still fails

Also tried declaring item as variant / double
Still same error
 
Upvote 0
You're searching column A but specifying to start after a cell in column B. Change, or remove, the After argument.
 
Upvote 0
You're searching column A but specifying to start after a cell in column B. Change, or remove, the After argument.

Well spotted, thankyou
Don't know how I missed that
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top