Application.WorksheetFunction.SumProduct具有4个数组的wsf.CountIfs

我试图让一个Application.WorksheetFunction.SumProduct wsf.CountIfs与4个数组一起工作。 到目前为止,我总是得到不正确的结果a1和b1 🙁

这是我的代码:

Dim lastrow As Long Dim wsf lastrow = Sheet2.Cells(Sheet2.Rows.Count, "M").End(xlUp).Row Set wsf = Application.WorksheetFunction Doctors = Array("Peter","Sam","Henry") Emergency = Array("Y","N") Specialty = Array("GP","Specialist") Rank = Array("Senior","Junior") a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), Emergency, Sheet2.Range("O2:O" & lastrow), Specialty, Sheet2.Range("R2:R" & lastrow), Rank)) b1 = Application.WorksheetFunction.SumProduct(wsf.SumIfs(Sheet2.Range("G2:G" & lastrow), Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), Emergency, Sheet2.Range("O2:O" & lastrow), Specialty, Sheet2.Range("R2:R" & lastrow), Rank)) 

我得到的最好的只是通过wsf.transpose使用2个标准:

 a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), wsf.Transpose(Emergency))) 

同样的不超过2个标准。

任何线索?

我可能得到了这个错误,但通过阅读你的代码,我知道你现在正在将SUMPRODUCT应用到4个数组 ,但是对于1个值 (一个),因为任何COUNTIFS的结果是一个整数而不是一个数组,只有你使用一个COUNTIFS,通过读取vba中的代码,只有一个为SUMPRODUCT函数声明的参数(在“a1”的最后一个括号将启动第二个参数/数组的声明语句之前添加“,”而不是5日)。 因此,在代码中声明的4个数组用于计算符合条件的no。 的细胞/案例不是SUMPRODUCT函数的数组。 因此如果:

a)你想将SUMPRODUCT应用到4个整数(???冗余),你应该使用4 COUNTIFS如下:

 Dim CountDoctors, CountEmergency, CountSpecialty, CountRank As Integer CountDoctors = wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors) CountEmergency = wsf.CountIfs(Sheet2.Range("M2:M" & lastrow), Emergency) CountSpecialty = wsf.CountIfs(Sheet2.Range("O2:O" & lastrow), Specialty) CountRank = wsf.CountIfs(Sheet2.Range("R2:R" & lastrow), Rank) a1 = Application.WorksheetFunction.SumProduct(CountDoctors, CountEmergency, CountSpecialty, CountRank) ' In this case the SUMPRODUCT does nothing special and you can simply use ' a1 = CountDoctors + CountEmergency + CountSpecialty + CountRank 

B)。 你想使用4个范围/数组(如使用SUMPRODUCT的真实function一样),你应该用一个代码过滤PMOR列(如果需要的话可以提供),然后创build“合格”的sumproduct范围/数组,基于第5,第6和数据集中的第X列,其值为数字(长),如工作小时数,薪金支付,没有。 耐心,加class等

你想用什么SUMPRODUCT函数? 也许有一个解决scheme为COUNTIFFS上的SUBPRODUCT