I am trying to return the row number of cells that match two criteria taken from a different worksheet than those of the ranges.
Dim WS As Worksheet
Dim WP As Worksheet
Dim namerange As Range
Dim frange As Range
Dim LRP As Integer
Dim p As Integer
Dim place As Integer
Set WP = Sheets("Pacients")
Set WS = Sheets("Agenda")
Set namerange = WS.Range("C2:C5000")
Set frange = WS.Range("G2:G5000")
LRP = WP.Cells(Rows.Count, 1).End(xlUp).Row
p = 1
For p = 1 To LRP
WP.Activate
pacient = Range("B" & p)
If Range("O" & p).Value = True Then
Do While Range("O" & p).Value = True
WP.Activate
place = WorksheetFunction.Match(1, (namerange = Range("B" & p)) * (frange <> "F"), 0)
WS.Activate
Range("J" & place).Select
ActiveCell.FormulaR1C1 = "1"
Range("J" & place + 1).Select
ActiveCell.FormulaR1C1 = "=IF(RC[-7] =" & pacient & " & MONTH(RC[-8])=MONTH(R" & place & "C2),1,"""")"
Range("J" & place + 1).Select
Selection.Copy
Range("J" & place + 1 & ":J5000").Select
ActiveSheet.Paste
WP.Activate
Range("O" & p).Calculate
Loop
End If
Next p
Specifically, the part of the code that gives a mismatch error is place = WorksheetFunction.Match(1, (namerange = Range("B" & p)) * (frange <> "F"), 0)
Note that namerange
and frange
are from WS worksheet and Range("B" & p)
comes from WP worksheet.
I have searched online and found an alternative using WS.Evaluate("MATCH(,,))
but then I don't know how to change worksheet from criteria to range.
Also, given the if statement If Range("O" & p).Value = True
the match function should return a number always (I did this to avoid type mismatches between integers and error).
Any help would be very much appreciated!