根据多个条件计数

所以我看了count&countif,但没有看到我正在努力完成什么。 基本上我有3列的数据。 销售人员,城市,date

数据可能如下所示:

SM1 City1 07/01/2010 SM2 City1 07/01/2010 SM1 City2 07/01/2010 SM3 City1 07/01/2010 

我想添加一个第四列,显示当天有多less销售人员访问该城市。 所以我想最终:

 SM1 City1 07/01/2010 3 SM2 City1 07/01/2010 3 SM1 City2 07/01/2010 1 SM3 City1 07/01/2010 3 

所以我需要根据城市和date来计算数量。 任何帮助,将不胜感激。 只是一些指针将是最受欢迎的。 我想使用VBA,并能够dynamic地确定行数,因为它不会是静态的。

我不确定为什么你需要VBA,简单的COUNTIFS公式可以为你工作。 使用您提供的示例数据,请在D1单元格中使用此公式并复制下来:

 =COUNTIFS(B:B,B1,C:C,C1) 

我同意@tigeravatar公式=COUNTIFS(B:B,B1,C:C,C1)会为你做。 只要双击列D中最后一行的右下angular,并让你填写。

如果你不pipe用什么理由使用VBA,你可以尝试这样的事情,假设你已经在列D的最后一行有上述公式:

 Sub countIf() 'Get the last row in the sheet, presumably where your A/B/C columns end sheetLastRow = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row 'Get the last row of the D column where your formula should be countIfLastrow = ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row 'Copy the formula down the D column to the end of your A/B/C columns, ' just like clicking the corner of the cell ActiveSheet.Range("D" & countIfLastrow).AutoFill Destination:=ActiveSheet.Range("D" & countIfLastrow & ":D" & sheetLastRow) End Sub