wsf.CountIfs数组Debacle – VBA Excel

我面临着一个我不能解释的奇怪问题。

我正在使用Worksheet函数以及CountIfs公式,而且我也使用Sumproduct来使用数组。

但是,每次我尝试使用2个不同的variables定义为数组我得到不正确的结果。

让我解释,

当我使用:

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") a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), Emergency)) 

我得到了a1错误的结果。

但是,当我尝试:

 a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), "Y")) b1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), "N")) Final = a1 + b1 

要么

 a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), "Peter", Sheet2.Range("M2:M" & lastrow), Emergency)) b1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), "Sam", Sheet2.Range("M2:M" & lastrow), Emergency)) c1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), "Henry", Sheet2.Range("M2:M" & lastrow), Emergency)) Final = a1 + b1 + c1 

我得到了Final的正确结果。

有没有办法让第一个公式工作,或者vba是否允许在一个countifs函数中使用多个variables作为标准。

我想也许我应该宣布医生和紧急variables,但目前为止没有运气。

有什么build议么?

除非在2个标准中有相同数量的项目,否则你的公式将不起作用。 (在这种情况下,您需要例如YN和Z来匹配医生的数量)。
我会build议UDF可能是最简单的解决scheme。

我不认为在DoctorsEmergency有相同数量的项目可以解决这个问题 – 如果你在紧急情况下增加了“Z”,例如你只能得到3个组合的数量,Peter / Y,Sam / N和亨利/ Z不是所需的3 * 3 = 9的组合。

我比VBA更好的公式,所以我不知道这是最好的解决scheme,但你可以转置一个数组(不pipe它们是否大小相同),然后给你所有的组合(3 * 2 = 6在你的例子),即改变为

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

如果把它写成工作表函数,你可以这样做:

=SUMPRODUCT(COUNTIFS(Sheet2!P2:P100,{"Peter","Sam","Henry"},Sheet2!M2:M100,{"Y";"N"}))

请注意,我没有使用TRANSPOSE ,我只是在{“Y”;“N”}中使用了一个分号分隔符,它有效地将它从“行”转换为“列” – 我不知道是否你可以通过在VBA中以不同方式定义数组来实现这一点…….

你可以在VBA中使用数组而不是函数:

 Option Explicit Option Compare Text Sub MediCount() ' Dim lastrow As Long Dim Doctors As Variant Dim Emergency As Variant Dim Profession As Variant Dim vData As Variant Dim Ct As Long Dim jDoc As Long Dim jEmr As Long Dim jPro As Long Dim j As Long ' lastrow = Sheet1.Cells(Sheet1.Rows.Count, "M").End(xlUp).Row Doctors = Array("Peter", "Sam", "Harry") Emergency = Array("Y", "N") Profession = Array("Teacher", "Accountant", "Plumber", "Artist") ' assume emergency in M, Profession in N, Doctors in O, start row=2 vData = Sheet1.Range("M2").Resize(lastrow, 3).Value2 For j = LBound(vData) To UBound(vData) For jDoc = LBound(Doctors) To UBound(Doctors) If vData(j, 3) = Doctors(jDoc) Then For jEmr = LBound(Emergency) To UBound(Emergency) If vData(j, 1) = Emergency(jEmr) Then For jPro = LBound(Profession) To UBound(Profession) If vData(j, 2) = Profession(jPro) Then Ct = Ct + 1 End If Next jPro End If Next jEmr End If Next jDoc Next j MsgBox Ct End Sub 

(或者你可以使用我的SpeedTools ACOUNTIFS函数=ACOUNTIFS(0,$M$2:$O$50,1,1,$H$2:$H$5,2,$I$2:$I$5,3,$G$2:$G$5)

但这是一个商业Excel插件)