Countif与下一个单元格不空

我需要计算一个string在一个范围内出现的次数,但只有当下一个单元格不为空时。

A 1 B 5 D 4 A G 1 B 4 B 8 D 

所以我想要A-> 1,B-> 3,D-> 1,G-> 1

我怎样才能做到这一点?

如果你正在寻找一个细胞的公式,这应该得到你所需要的:

 =COUNTIFS(A1:A8,"A",B1:B8,"<>") 

A1:A8是你的信件栏, B1:B8是你的号码栏。

*注意这是COUNTIFS() (与S ),而不是COUNTIF() (没有S )。


为了完整起见,这也是可行的,但是可能比你想要的更复杂:

 =SUMPRODUCT((A1:A8="A")*(LEN(B1:B8)>0)) 

如果你正在寻找一个VB(A)解决scheme,这应该工作:

 For i = 1 To 8 'Replace mySheet and CellCount with the proper variables for your use If mySheet.Range("A" & i).Value2 = "A" And Len(mySheet.Range("B" & i)) > 0 Then CellCount = CellCount + 1 End If Next i