0

別のセルの値に基づいて値を合計する VBA の方法を探しています。

ソース表の例:

 BillNr   Pos   Value

  200        0  sum
  200        1  10,00 €
  200        2  15,00 €
  200        3  31,00 €
  200        4  21,00 €
  200        5  19,00 €
  200        6  81,00 €
  201        0  sum
  201        1  14,00 €
  201        2  18,00 €
  212        0  sum
  212        1  31,00 €
  212        2  19,00 €
  212        3  78,00 €

VBAコードの数値に応じて、BillNrすべての値を合計し、呼び出されたセルにValue表示する必要があります。実際には、リストは行の長さに関するものなので、ループでラップする必要があると思いますか?Numbersum15000

4

1 に答える 1

0

好きなのを選びな :)

VBA

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim aCell As Range, bCell As Range
    Dim ExitLoop As Boolean

    '~~> Change this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Find "sum" in Col C
        Set aCell = .Columns(3).Find(What:="sum", LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        If Not aCell Is Nothing Then
            Set bCell = aCell

            '~~> If found Calculate the sum
            aCell.Value = Application.Evaluate("=SUMIF(A:A,A" & aCell.Row & ",C:C)")

            '~~> Find the next "Sum"
            Do While ExitLoop = False
               Set aCell = .Columns(3).FindNext(After:=aCell)

               If Not aCell Is Nothing Then
                   If aCell.Address = bCell.Address Then Exit Do
                   aCell.Value = Application.Evaluate("=SUMIF(A:A,A" & aCell.Row & ",C:C)")
               Else
                   ExitLoop = True
               End If
            Loop
        End If
    End With
End Sub

スクリーンショット

ここに画像の説明を入力

非 VBA (ピボットと Vlookup を使用)

Vlookupピボットを作成した後、Col D で使用しています。フィルターを設定Autofilterしてから、関連するセルに数式を入れることができますCol CsumCol C

ここに画像の説明を入力

于 2013-01-18T11:18:06.733 に答える