VBA: MATCH with multiple criteria to return row number

MadeFromMetal

New Member
Joined
Sep 26, 2017
Messages
5
I'm trying to use VBA to use MATCH to return a row number. Then use that row number in the same Worksheet_Change macro.

Code:
If Intersect(Target, Range("A2:A3000")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
Dim lc As Long, fr As Long, n As Long, nr As Long
Dim fc As Range, sc As Range, stl1 As Range, stl2 As Range
Set fc = Cells(Target.Row, 1)
Set sc = Cells(Target.Row, 2)
Set stl1 = Range("A1", Range("A" & Rows.Count).End(xlUp).Offset(-1))
Set stl2 = Range("B1", Range("B" & Rows.Count).End(xlUp).Offset(-1))

With Application
  .EnableEvents = False
  .ScreenUpdating = False
  n = Application.CountIfs(Columns(1), fc, Columns(2), sc)
  If n = 1 Then
    lc = Cells(Target.Row, Columns.Count).End(xlToLeft).Column
    If lc > 1 Then
      Cells(Target.Row, lc + 1) = Format(Now, "m/d/yyyy h:mm")
    End If
  Else
    fr = 0
    On Error Resume Next
    [B]fr = Application.WorksheetFunction.Match(fc & sc, stl1 & stl2)[/B]
    Debug.Print fr
    On Error GoTo 0
    If fr > 0 Then
      lc = Cells(fr, Columns.Count).End(xlToLeft).Column
      If lc = 2 Then
        Cells(fr, lc + 1) = Format(Now, "m/d/yyyy h:mm")
      ElseIf lc = 3 Then
        Cells(fr, lc + 1) = Format(Now, "m/d/yyyy h:mm")
    End If
      Cells(Target.Row, 1).ClearContents
      Cells(Target.Row, 2).ClearContents
    End If
  End If
On Error Resume Next
  Me.Range("A1", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  On Error GoTo 0
  nr = Me.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
  Me.Cells(nr, 1).Select
  .EnableEvents = True
  .ScreenUpdating = True
End With

End Sub

When the bolded code reads
Code:
fr = Application.WorksheetFunction.Match(fc, stl1)
The Worksheet_Change macro works fine until there happens to be a duplicate value in column A. I need it to match the values in columns A & B respectively and make the changes in the appropriate row.

When the bolded code reads as is, the debug.print returns a 0.

I have also tried

Code:
fr = Application.WorksheetFunction.Evaluate("=MATCH(" & fc & "&" & sc & "," & stl1 & "&" & stl2 & ")")

Any help is appreciated!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You refer to value in stead of address.
fc ==> fc.Address
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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