2016-08-31 33 views
-1

我想使用countif語句來計算僱員人數(在我的示例中),他們的性別是男性,他們的年齡在15到30之間。我不確定如何向countif語句添加另一個參數以添加限制性別,因爲它只需要2個陳述。 這裏是我的功能代碼:如何在VBA中使用超過2個參數的countif?

Function Calculate(Column_target As Integer, Row_number As Double, Level_of_response As Integer, Column_of_reference As Integer, level_of_reference As Integer) As Double 

    Sheets("A").Select 
    Range(Cells(2, Column_target), Cells(Row_number , Column_target)).Select 

    For i = 3 To Row_number + 1 

     If (Level_of_response = 1) Then 
     Calculate = (Application.WorksheetFunction.CountIf(Selection, "<31") - Application.WorksheetFunction.CountIf(Selection, "<15"))/(Row_number - 1) 

     End If 

這是我想補充的第三個參數爲COUNTIF語句的代碼。

(Cells(i, Column_of_reference) = level_of_reference)'這表明當level_of_reference是1(或2)時Gender是男性(或女性)。

回答

2

你必須求助於Countifs,有任何條件。

此外,您可以鞏固兩個數值的條件爲一個(ABS((X-(XMIN XMAX +)/ 2)<(XMAX,XMIN)/ 2)。

+0

還有就是「不能一埃羅到獲得工作表功能類的countif屬性「! – Zapata

+0

@ Zapata - 也許[this](http://stackoverflow.com/a/12009904/2707864)幫助。 –

+0

@Zapata - 或[this](http://www.mrexcel .com/forum/excel-questions/584979-runtime-error-1004-unable-get-countif-property-worksheetfunction-class-please-help.html)試試'Application.CountIfs(...'而不是'Application .WorksheetFunction.CountIfs(...'。 –