This is the structure of my workbook.
One sheet called "export" has all the roomKeys per hotel (they can be repeated) (2k lines).
One sheet called "delete" has all the roomkeys to be deleted (100 lines).
What I want is from sheet "delete" create a function where you pass as a parameter the "roomkey" and it will tell you how many times that value is repeated in the column roomkey from the "export" sheet.
My idea is to filter by roomkey and then count how many lines in the selection and return that number. I think I can't call another worksheet inside a function.
Public Function HowManyRatePlansPerRoom(Roomkey As String) As Integer
Dim SheetName As String
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim LastRow As Integer
Dim Rowz As Integer
On Error GoTo errorHandler
'Application.ScreenUpdating = True
Set ws1 = Sheets("Export")
ws1.Activate
ws1.Cells(1, 1).Select
'Find the last row
LastRow = ws1.Range("A1").CurrentRegion.Rows.Count
'select the data set
ws1.Range("A1:BI" & CStr(LastRow)).Select
'filter table by roomkey
Selection.AutoFilter Field:=1, Criteria1:=Roomkey
'this counts the data in the filter and -1 means minus the header
Rowz = ws1.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
HowManyRatePlansPerRoom = Rowz
errorHandler:
MsgBox "The following error occurred: " & Err.Description
Err.Clear
End Function
The context for that idea is if the roomkey is 5 times repeated on "export" and I have 5 "roomkeys" on "delete" I need to delete that room. On the other hand if I have 5 roomkeys on "delete" but only two on "export" I must leave the room.