1

初めてVBAコードを使ってみたのですが、下書きコードを書く前にいろいろ検索したのですが、うまく動かないものが1つ。Application.WorkSheetFunction.CountIfs を使用しましたが、コードは非常に遅く、140.000 行と 31 列のテーブルを読み取る必要があり、countif の基準を持つテーブルには 6000 行と 13 列があります。

Sheet2.Cells(x, 17)、Sheet2.Cells(x, 14)、Sheet2.Cells(x, 17)のコードは間違っていますが、問題が見つかりません

countIfs に使用するコードに従ってください

x = 2 y = Application.CountA(Range("A:A"))

Dim Submit_Date As Range
Dim GU As Range
Dim Legal_Entity_Country As Range
Dim Media_Type As Range
Dim Doc_Status As Range
Dim Approval_Date As Range
Dim Month_Create As Range
Dim FY As Range
Dim Status As Range

Set Submit_Date = Range("Table1[[#All],[Submit_Date]]")
Set GU = Range("Table1[[#All],[GU]]")
Set Legal_Entity_Country = Range("Table1[[#All],[Legal_Entity_Country]]")
Set Media_Type = Range("Table1[[#All],[Media_Type]]")
Set Doc_Status = Range("Table1[[#All],[Doc_Status]]")
Set Approval_Date = Range("Table1[[#All],[Approval_Date]]")
Set Month_Create = Range("Table1[[#All],[Month_Create]]")
Set FY = Range("Table1[[#All],[FY]]")
Set Status = Range("Table1[[#All],[Status]]")

For x = 2 To y
    With Application.WorksheetFunction
        Sheet2.Cells(x, 10) = _
            .Sum(.CountIfs(Month_Create, Sheet2.Cells(x, 3), _
                           FY, Sheet2.Cells(x, 5), _
                           GU, Sheet2.Cells(x, 6), _
                           Legal_Entity_Country, Sheet2.Cells(x, 7), _
                           Media_Type, Sheet2.Cells(x, 8), _
                           Doc_Status, Sheet2.Cells(x, 9), _
                           Status, Sheet2.Cells(1, 10)), _
                 .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), _
                           GU, Sheet2.Cells(x, 6), _
                           Legal_Entity_Country, Sheet2.Cells(x, 7), _
                           Media_Type, Sheet2.Cells(x, 8), _
                           Doc_Status, Sheet2.Cells(x, 9), _
                           Status, Sheet2.Cells(1, 10)), _
                 .CountIfs(Approval_Date, ">" & Sheet2.Cells(x, 2), _
                           Month_Create, Sheet2.Cells(x, 3), _
                           FY, Sheet2.Cells(x, 5), _
                           GU, Sheet2.Cells(x, 6), _
                           Legal_Entity_Country, Sheet2.Cells(x, 7), _
                           Media_Type, Sheet2.Cells(x, 8), _
                           Doc_Status, Sheet2.Cells(x, 9), _
                           Status, Sheet2.Cells(1, 13)), _
                 .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), _
                           Approval_Date, ">=" & Sheet2.Cells(x, 1), _
                           GU, Sheet2.Cells(x, 6), _
                           Legal_Entity_Country, Sheet2.Cells(x, 7), _
                           Media_Type, Sheet2.Cells(x, 8), _
                           Doc_Status, Sheet2.Cells(x, 9), _
                           Status, Sheet2.Cells(1, 13)))
         Sheet2.Cells(x, 11) = .Sum(.CountIfs(Month_Create, Sheet2.Cells(x, 3), FY, Sheet2.Cells(x, 5), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 11)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 11)))
         Sheet2.Cells(x, 12) = .Sum(.CountIfs(Month_Create, Sheet2.Cells(x, 3), FY, Sheet2.Cells(x, 5), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 12)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 12)))
'         Sheet2.Cells(x, 13) = .Sum(.CountIfs(Month_Create, Sheet2.Cells(x, 3), Approval_Date, "<=" & Sheet2.Cells(x, 2), FY, Sheet2.Cells(x, 5), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 13)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), Month_Approved, Sheet2.Cells(x, 3), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 13)))
'         Sheet2.Cells(x, 14) = .Sum(.CountIfs(Month_Create, Sheet2.Cells(x, 3), FY, Sheet2.Cells(x, 5), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 14)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), Month_Approved, Sheet2.Cells(x, 3), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 14)))
         Sheet2.Cells(x, 15) = .Sum(Sheet2.Cells(x, 10), Sheet2.Cells(x, 11), Sheet2.Cells(x, 12), Sheet2.Cells(x, 13), Sheet2.Cells(x, 14))
         Sheet2.Cells(x, 16) = .CountIfs(Month_Create, Sheet2.Cells(x, 3), FY, Sheet2.Cells(x, 5), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9))
'         Sheet2.Cells(x, 17) = .Sum(.CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), Month_Approved, Sheet2.Cells(x, 3), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 14)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 11)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 12)))
         Sheet2.Cells(x, 18) = .Sum(.CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 10)), .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), Approval_Date, ">=" & Sheet2.Cells(x, 1), GU, Sheet2.Cells(x, 6), Legal_Entity_Country, Sheet2.Cells(x, 7), Media_Type, Sheet2.Cells(x, 8), Doc_Status, Sheet2.Cells(x, 9), Status, Sheet2.Cells(1, 13)))
         Sheet2.Cells(x, 19) = .Sum(Sheet2.Cells(x, 16), Sheet2.Cells(x, 17), Sheet2.Cells(x, 18)) = Sheet2.Cells(x, 15)
    End With
Next x

よろしく アンドレ

4

1 に答える 1

1

基準が何であるかを見ると、基準を変更することでパフォーマンスが大幅に向上することがわかります。1 番目または 2 番目の基準を除いて、それらはすべて、同じ複数の基準にわたって等しいかどうかをチェックしています。

チェックするセルを (区切り記号で) 連結する新しい列に値を追加します。次に、同様に連結された基準に対してそれらをチェックします。例として、各行の GU、Legal_Entity_Country、Media_Type、Doc_Status、および Status が特定の値に等しいかどうかをすべてチェックしています。したがって、それらを結合してから、結合基準に対して結合を確認します。

したがって、数式を使用する場合は、「=CONCATENATE(A1,"|",B1,"|",C1,"|",D1)」などの列を追加します。ここで、A1、B1 などは関連する値です。行。次に、あなたの基準は、同様に連結された関連する基準になります。次に、countifs を =COUNTIFS( _ ,combinedColumn,combinedCritera) のように変更します。

これを行うと、ルックアップの数が大幅に減少し、それに応じて速度が向上します。

マクロでそれを変更するには、次のようにします(質問のコードから抽出した例にすぎません)

年:

.CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), _
                           GU, Sheet2.Cells(x, 6), _
                           Legal_Entity_Country, Sheet2.Cells(x, 7), _
                           Media_Type, Sheet2.Cells(x, 8), _
                           Doc_Status, Sheet2.Cells(x, 9), _
                           Status, Sheet2.Cells(1, 10)), _

新しい:

Dim combinedCriterion As String
combinedCriterion = Join(Array(Sheet2.Cells(x, 7),Sheet2.Cells(x, 8),Sheet2.Cells(x, 9),Sheet2.Cells(1, 10)),"|")
     .CountIfs(Submit_Date, "<" & Sheet2.Cells(x, 1), _
                               combinedColumn, combinedCriterion)
...etc
于 2013-07-27T00:33:38.657 に答える