worksheet function match a named range in VBA

juanbolas

New Member
Joined
Dec 3, 2014
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to write code that will check if the cell values have changed in a given range, and if so check if the value is in a list. If it´s not on the list it should open up a form to enter the new suppliers information.

I'm running into a problem trying to match the value entered in the cell to the list. the error i get is "Run-time error 1004: Unable to get the Match property of the WorksheetFunction class".

Code follows. Any help is appreciated.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range) 'Excel VBA with more cells in the range.
If Not Intersect(Target, Range("Proveedores_Input")) Is Nothing Then
    If WorksheetFunction.Match(Target.Value, Sheets("Proveedores").Range("C5:C1000000"), 0) Then
'    If WorksheetFunction.Match(Target.Value, Sheets("Proveedores").Range("Proveedores_Lista"), 0) Then
        MsgBox Target.Value & " - Matched"
    End If
End If
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
2,997
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
WorksheetFunction.Match will return that error if it doesn't find a match.
I use
VBA Code:
myMatch = Application.Match(Target.Value, Sheets("Proveedores").Range("C5:C1000000"), 0)
If IsError(myMatch) Then
'   what to do if no match found
Else
'   what to do if there is a match
End If

myMatch has to be dimmed as variant

Beware that your code doesn't consider the possibility that more than one cell get changed
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
1,810
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Another option
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range) 'Excel VBA with more cells in the range.
    Dim rngResult As Range
    
    If Not Intersect(Target, Range("Proveedores_Input")) Is Nothing And Target.Cells.Count = 1 Then
        Set rngResult = Sheets("Proveedores").Range("C5:C1000000").Find(What:=Target.Value, LookAt:=xlWhole)
        
        If Not rngResult Is Nothing Then
            MsgBox Target.Value & " - Matched"
        Else
            MsgBox Target.Value & " - NOT Matched"
        End If
    End If
End Sub
 

Forum statistics

Threads
1,176,499
Messages
5,903,413
Members
435,028
Latest member
excelvbanoob420

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
Top