2

Application.WorksheetFunction.SumProduct wsf.CountIfs を 4 つの配列で動作させようとしています。これまでのところ、a1 と b1 の結果は常に正しくありません:(

これは私のコードです:

Dim lastrow As Long
Dim wsf
lastrow = Sheet2.Cells(Sheet2.Rows.Count, "M").End(xlUp).Row
Set wsf = Application.WorksheetFunction

Doctors = Array("Peter","Sam","Henry")
Emergency = Array("Y","N")
Specialty = Array("GP","Specialist")
Rank = Array("Senior","Junior")

a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), Emergency, Sheet2.Range("O2:O" & lastrow), Specialty, Sheet2.Range("R2:R" & lastrow), Rank))

b1 = Application.WorksheetFunction.SumProduct(wsf.SumIfs(Sheet2.Range("G2:G" & lastrow), Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), Emergency, Sheet2.Range("O2:O" & lastrow), Specialty, Sheet2.Range("R2:R" & lastrow), Rank))

私がしなければならない最善のことは、wsf.transposeを通じて2つの基準のみを使用することです:

a1 = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors, Sheet2.Range("M2:M" & lastrow), wsf.Transpose(Emergency))) 

同じことは、2 つ以上の基準では機能しません。

手がかりはありますか?

4

1 に答える 1

0

私はこれを間違っているかもしれませんが、あなたのコードを読むことで、SUMPRODUCT を4 つの配列はなく1 つの値(1) に適用していることがわかります。 1 つの COUNTIFS を使用し、vba でコードを読み取る、SUMPRODUCT 関数に対して宣言された引数が 1 つだけになります (「a1」の最後の括弧の前に「,」を追加すると、2 番目の引数/配列の宣言ステートメントが開始されます。 5番目)。そのため、コードで宣言されている 4 つの配列は、適格な番号のカウントに使用されます。SUMPRODUCT 関数の配列としてではないセル/ケースの。したがって、次の場合:

a) SUMPRODUCT を 4 つの整数 (??? 冗長) に適用する場合は、以下のように 4 つの COUNTIFS を使用する必要があります。

Dim CountDoctors, CountEmergency, CountSpecialty, CountRank As Integer
CountDoctors = wsf.CountIfs(Sheet2.Range("P2:P" & lastrow), Doctors)
CountEmergency = wsf.CountIfs(Sheet2.Range("M2:M" & lastrow), Emergency)
CountSpecialty = wsf.CountIfs(Sheet2.Range("O2:O" & lastrow), Specialty)
CountRank = wsf.CountIfs(Sheet2.Range("R2:R" & lastrow), Rank)
a1 = Application.WorksheetFunction.SumProduct(CountDoctors, CountEmergency, CountSpecialty, CountRank)
' In this case the SUMPRODUCT does nothing special and you can simply use
' a1 = CountDoctors + CountEmergency + CountSpecialty + CountRank

b)。4 つの範囲/配列を使用する場合 (SUMPRODUCT の真の機能を使用する場合と同様)、列 PMOR をコードでフィルター処理し (必要に応じて提供できます)、5 番目、6 番目に基づいて「適格な」合計範囲/配列を作成する必要があります。データセットの X 番目の列は、勤務時間、給与支払額、いいえなどの数値 (長い) として値を持ちます。我慢、残業など。

SUMPRODUCT 関数を何に使用しますか? COUNTIFS の SUBPRODUCT の回避策があるかもしれません

于 2013-11-12T22:41:02.317 に答える