簡體   English   中英

如何編寫COUNTIF函數以在一列中計數並使用多列中的條件?

[英]How do I write a COUNTIF function to count in one column and use criteria from multiple columns?

我正在努力整理一份股票報告,這是我在之前的一個問題中提到過的。 其中一個字段用於計算當前正在運送的項目,但需要使用與介紹日期相關的單獨列中的條件(UDF_INTRODUCED)。

我使用COUNTIF函數在過去6個月內獲得“UDF_INTRODUCED”的計數,現在需要獲得該群體中具有“FirstOfDuedate1”的項目數。 我不知道這是一個嵌套的COUNTIF,還是我需要使用COUNTIFS。

源數據的圖像:

源數據頭和7行

當前輸出的圖像,需要獲得字段B7的數值:

電流輸出

我正在嘗試使用COUNTIFS函數根據多個條件進行計數,如下面代碼末尾所示:

Sub CommandButton1_Click()

Range("A2").Value = "Total FG"
Range("A3").Value = "Total out of stock"
Range("A4").Value = "Total out of stock intro past 6 months (included in 
Total out of stock)"
Range("A5").Value = "Total out of stock less past 6 months introductions"
Range("A6").Value = "Percentage in stock not included new introductions"
Range("A7").Value = "Items in Transit"
Range("A8").Value = "Percentage in stock including items in transit"

Dim MyPath As String, mps As Variant, mps_temp As String, mydate As Date, 
IntroDate As Date, i As Integer

MyPath = "C:\Users\Kirank\Documents\Stock Feed 
Analysis\HVL_Available_to_Sell_Report_with_Headers 2019.01.01"
mps = Split(MyPath, " ")

For i = LBound(mps) To UBound(mps)
    mps_temp = mps(UBound(mps) - i)
    If mps_temp Like "####.##.##" Then
        mydate = DateSerial(Mid(mps_temp, 1, 4), Mid(mps_temp, 6, 2), 
Mid(mps_temp, 9, 2))
        IntroDate = mydate - 181
        Exit For
    End If
Next
Range("B1").Value = mydate

    Range("B2").Select
    ActiveCell.FormulaR1C1 = 
"=COUNT(HVL_Available_to_Sell_Report_wi!C[3])"
    Range("B3").Select
    ActiveCell.FormulaR1C1 = 
"=COUNTIF(HVL_Available_to_Sell_Report_wi!C[3],0)"
    Range("B4").Select
    ActiveCell.FormulaR1C1 = 
"=COUNTIF(HVL_Available_to_Sell_Report_wi!C[13],"">=" & IntroDate & """)"
    Range("B5").Select
    ActiveCell.FormulaR1C1 = Range("B3") - Range("B4")
    Range("B6").Select
    ActiveCell.FormulaR1C1 = (Range("B2") - Range("B5")) / Range("B2")
    Selection.Style = "Percent"
    Range("B7").Select
    ActiveCell.FormulaR1C1 = 
"=COUNTIFS(HVL_Available_to_Sell_Report_wi!C[4]," > " & 0" & 
"HVL_Available_to_Sell_Report_wi!C[13],"">=" & IntroDate & """)"

End Sub

輸出結果為“TRUE”而不是數字。 最終值應為45。

問題在於此代碼: > " & 0"

你希望有一個> 0的條件,但你的引號的方式意味着>符號在所有它們之外,所以你基本上做一個比較操作。 這就是為什么你要True

嘗試這個:

ActiveCell.FormulaR1C1 = "=COUNTIFS(HVL_Available_to_Sell_Report_wi!C[4], "">0"",HVL_Available_to_Sell_Report_wi!C[13],"">=" & IntroDate & """)"

暫無
暫無

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

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