簡體   English   中英

根據多個條件進行計數

[英]Count based on multiple conditions

因此,我查看了count&countif,但未完全了解我要完成的工作。 基本上我有3列數據。 售貨員,城市,日期

數據可能如下所示:

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

我想添加第4列,該列顯示在該日期訪問該城市的銷售人員人數。 所以我想結束:

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

因此,我需要根據城市和日期來計算計數。 任何幫助,將不勝感激。 只有一些指針是最受歡迎的。 我想使用VBA並能夠使其動態確定行數,因為它不是靜態的。

我不確定為什么您需要VBA,一個簡單的COUNTIFS公式將為您服務。 使用提供的示例數據,在單元格D1中使用此公式並復制下來:

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

我同意@tigeravatar的觀點,公式=COUNTIFS(B:B,B1,C:C,C1)將為您完成。 只需雙擊D列最后一行的右下角,然后為您填充即可。

如果由於某種原因而決定使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM