我在Excel 2011中使用VBA(但保存到Excel 97 - 2004)来计算C列中值为training
的所有匹配值。为此,我使用以下代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
Dim r As Range
Dim c
Set r = Sheets("Sheet2").Range("C:C")
c = Application.WorksheetFunction.CountIf(r, "training")
MsgBox "column C has " & c & " instances of 'training'"
End If
End Sub
我想要做的是创建一个CountIfs语句,允许我检查值" training"存在于C列中,如果值10
存在于另一列B中,并且两者都在同一行内匹配,则计算存在两个值的所有行,否则不计算。
请有人帮我解决这个问题吗?
答案 0 :(得分:0)
与Count if two criteria match - EXCEL formula类似,您需要COUNTIFS
函数
=COUNTIFS(B2:B7, 10, C2:C7, "training")
修改强>
没有看到有关Excel版本的更新; COUNTIFS
适用于2007 +
SUMPRODUCT
在2004年之前工作吗?
=SUMPRODUCT((B2:B7=10)*(C2:C7="training"))
在VBA
中,我刚刚对此进行了测试,它对我有用:
myTest = Evaluate("=SUMPRODUCT((B2:B7=10)*(C2:C7=""training""))")
答案 1 :(得分:0)
我设法解决了这个问题。请参阅以下示例
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
Dim r As Range
Dim p As Range
Dim t As Range
Dim u As Range
Dim c
Set r = Sheets("Sheet2").Range("C:C")
Set p = Sheets("Sheet2").Range("B:B")
Set t = Sheets("Sheet1").Range("B1")
Set u = Sheets("Sheet1").Range("C1")
c = WorksheetFunction.CountIfs(r, t, p, u)
MsgBox "column C has " & c & " instances of 'training'"
End If
End Sub