Using VBA Application.WorksheetFunction.Match if not found question

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,345
Office Version
  1. 365
Platform
  1. Windows
I am trying to check to see if data already exist in a table by using the Application.WorksheetFunction.Match function.

And I am able to create a formula that works "If there is a Match".

Code:
Private Sub CommandButton2_Click()

Dim Find As Variant


'Sheet22.Range("B4").Value = Application.WorksheetFunction.Match(Sheet16.Range("B6").Value, Sheet22.Range("B6:B200"), 0)
Find = Application.WorksheetFunction.Match(Sheet16.Range("B6").Value, Sheet22.Range("B6:B200"), 0)

 If Find.Value > 1 Then
    
    Sheet16.Range("Buyer_List1").Copy
    Sheet22.Range("B5").End(xlDown).Offset(1).PasteSpecial
    Application.CutCopyMode = False
    
    Sheet22.Activate
    
    Else
    MsgBox "This Buyer ID is already included in the List"
    
End If


End Sub

how do i get this to work if it doesnt find a match?

Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
VBA Code:
Find = Application.Match(Sheet16.Range("B6").Value, Sheet22.Range("B6:B200"), 0)

 If Not IsError(Find) Then
 
Upvote 0
Solution
Hi,
see if these changes to your code do what you want

VBA Code:
Private Sub CommandButton2_Click()
    Dim BuyerID     As String
    Dim FindMatch   As Variant
    
    BuyerID = Sheet16.Range("B6").Value
    If Len(BuyerID) = 0 Then Exit Sub
    
    FindMatch = Application.Match(BuyerID, Sheet22.Range("B6:B200"), 0)
    
    'no match
     If IsError(FindMatch.Value) Then
        
        Sheet16.Range("Buyer_List1").Copy
        Sheet22.Range("B5").End(xlDown).Offset(1).PasteSpecial
        Application.CutCopyMode = False
        
    Else
        'match
        MsgBox BuyerID & Chr(10) & "This Buyer ID is already included in the List", 48, "BuyerID Exists"
        
    End If


End Sub

Dave
 
Upvote 0
Thank you both but I get a Run Time '424' Object required error. Its identifying line: "If IsError(FindMatch.Value) Then" if I use Dave's solution

and if I use Fluffs and the buyer ID doesnt already exist I get a run time 1004 unable to get the Match property of the WorksheetFunction class.

Not sure what I am doing wrong I thought this would be easy with the Match function. I would get a value if there was a match and not one if it didnt. making my head spin. lol
 
Upvote 0
correction and apologies: if mistyped Fuff's solution. it worked.

Thank you both
 
Upvote 0
Thank you both but I get a Run Time '424' Object required error. Its identifying line: "If IsError(FindMatch.Value) Then" if I use Dave's solution
my bad should have been

VBA Code:
If IsError(FindMatch) Then

Dave
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
works for me

On Error Resume Next
FindMatch = Application.Match(BuyerID, Sheet22.Range("B6:B200"), 0)
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,220,976
Messages
6,157,172
Members
451,401
Latest member
Msigwa

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