这是我的工作簿的结构。
一张名为“导出”的表包含每个酒店的所有房钥匙(它们可以重复)(2k行)。
一张名为“删除”的工作表具有所有要删除的房间钥匙(100行)。
我想要的是从“删除”工作表中创建一个函数,在该函数中将“ roomkey”作为参数传递,它将告诉您“导出”工作表中的列roomkey中重复该值多少次。
我的想法是按Roomkey进行过滤,然后计算选择项中的多少行并返回该数字。我想我不能在函数内调用另一个工作表。
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
该想法的上下文是,如果在“导出”上将Roomkey重复5次,并且在“ delete”上有5个“ roomkeys”,则需要删除该房间。另一方面,如果我在“删除”上有5个房间钥匙,但在“出口”上只有两个,我必须离开房间。
答案 0 :(得分:0)
尝试一下:
Public Function Roomkey_Count(sRoomkey As String) As Byte
Dim bOutput As Byte
Dim rg As Range, lRow As Long
With ThisWorkbook.Sheets("Export")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rg = .Cells(1).Resize(lRow)
End With
bOutput = WorksheetFunction.CountIf(rg, sRoomkey)
Roomkey_Count = bOutput
End Function