我正在尝试执行满足两个条件的CountIfs函数。第一行是“已批准”,第二行是“批准日期”。当仅第一个条件存在时,CountIfs可以找到,但是当我添加第二个条件时,会出现Type Mismatch错误,我不确定为什么。
代码:
' Declarations
Dim sRoutine As String 'Routine’s Name
Dim lngStatus As Long
Dim lngLastRow As Long
Dim intRptMnth As Integer
Dim intRptYr As Integer
Dim lngAppvDate As Long
' Initialize Variables
lngLastRow = FindLastRow(strPSR_File, strCCL, 1)
lngStatus = Worksheets(strCCL).Range(FindLoc(strPSR_File, strCCL,"status")).Column
lngAppvDate = Worksheets(strCCL).Range(FindLoc(strPSR_File, strCCL,"Approved Date")).Column
intRptMnth = CInt(CalcRptMnthNum)
intRptYr = CalcRptYr
' Procedure
With Worksheets(strCCL)
CalcPCR_MTD_Cnt = Application.WorksheetFunction.CountIfs( _
Worksheets(strCCL).Range(Cells(2, lngStatus), Cells(lngLastRow,
lngStatus)), _
"=Approved", _
'********ERRORS HERE*****
Month(Worksheets(strCCL).Range("n2:n3")), _
intRptMnth)
'************************
End With
答案 0 :(得分:2)
更正的代码:
' Declarations
Dim sRoutine As String 'Routine’s Name
Dim lngStatus As Long
Dim lngLastRow As Long
Dim intRptMnth As Integer
Dim intRptYr As Integer
Dim lngAppvDate As Long
' Initialize Variables
lngLastRow = FindLastRow(strPSR_File, strCCL, 1)
lngStatus = Worksheets(strCCL).Range(FindLoc(strPSR_File, strCCL, "status")).Column
lngAppvDate = Worksheets(strCCL).Range(FindLoc(strPSR_File, strCCL, "Approved Date")).Column
intRptMnth = CInt(CalcRptMnthNum)
intRptYr = CalcRptYr
' Procedure
With Worksheets(strCCL)
CalcPCR_MTD_Cnt = Application.WorksheetFunction.CountIfs( _
Worksheets(strCCL).Range(.Cells(2, lngStatus), .Cells(lngLastRow, lngStatus)), _
"=Approved", _
Worksheets(strCCL).Range(.Cells(2, lngAppvDate), .Cells(lngLastRow, lngAppvDate)), _
">=" & DateSerial(intRptYr, intRptMnth, 1), _
Worksheets(strCCL).Range(.Cells(2, lngAppvDate), .Cells(lngLastRow, lngAppvDate)), _
"<" & DateSerial(Year(Date), Month(Date), 1) _
)
End With
答案 1 :(得分:2)
仅显示一种变体数组方法:
Dim sRoutine As String 'Routine’s Name
Dim lngStatus As Long
Dim lngLastRow As Long
Dim intRptMnth As Integer
Dim intRptYr As Integer
Dim lngAppvDate As Long
' Initialize Variables
lngLastRow = FindLastRow(strPSR_File, strCCL, 1)
lngStatus = Worksheets(strCCL).Range(FindLoc(strPSR_File, strCCL, "status")).Column
lngAppvDate = Worksheets(strCCL).Range(FindLoc(strPSR_File, strCCL, "Approved Date")).Column
intRptMnth = CInt(CalcRptMnthNum)
intRptYr = CalcRptYr
' Procedure
With Worksheets(strCCL)
Dim apprv As Variant
apprv = .Range(.Cells(2, lngStatus), .Cells(lngLastRow, lngStatus)).Value
Dim dte As Variant
dte = .Range(.Cells(2, lngAppvDate), .Cells(lngLastRow, lngAppvDate)).Value
CalcPCR_MTD_Cnt = 0
Dim i As Long
For i = LBound(apprv, 1) To UBound(apprv, 1)
If apprv(i, 1) = "Approved" And Month(dte(i, 1)) = intRptMnth And Year(dte(i, 1)) = intRptYr Then CalcPCR_MTD_Cnt = CalcPCR_MTD_Cnt + 1
Next i
End With