0

私は本当にどうすればよいかわからないことをやろうとしています。

私は2つのスプレッドシートsheet1とsheet2を持っています

シート 1 には、
列 A に数値 (SalespersonsID) のリストがあり
、列 B にはシート 2 の列 B の合計があります

SalespersonID | Total sales
--------------+------------
            1 | 
            2 |  

シート 2 にある
列 Aは salespersonsID を持っ
ている 列 B はそのトランザクションで販売員が販売したウィジェットの数を持っている
列 C は TypeofWidget を持っている
列 D は Location を持っている

SalespersonID | Units sold | Type | Location
--------------+------------+------+-----------
            1 |          1 | Foo  | London
            1 |          2 | Bar  | London
            2 |          4 | Foo  | Berlin
            1 |          1 | Bar  | Madrid

これを行う方法がわかりませんが、シート 2 の列 C と D およびシート 1 の販売 ID を条件として使用してセールスマンが販売したウィジェットの総数を挿入し、それをシート 1 の列 B に挿入する必要がありますか?

SalespersonID | Total sales
--------------+------------
            1 |           4
            2 |           4

SumIFS 関数を使用してセルで実行できますが、合計 5 枚のシートで 500 行を超える行があります。

4

1 に答える 1

0

これは、必要なジョブを実行する vba サブです。vba に入れて実行するだけです。

sub totalSales()
  Dim i As Integer 'used to loop on sheet1
  Dim j As Integer 'used to loop on sheet2
  Dim spID As Integer 'the SalepersonID in Sheets1
  Dim spID2 As Integer 'the SalepersonID in Sheets2
  Dim rows1 as Integer 'rows count in sheet1
  Dim rows2 as Integer 'rows count in sheet2
  Dim total as Integer 'to count sales per person

  'getting rows count
  rows1=Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row 'rows count in sheet1
  rows2=Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row 'rows count in sheet2

    For i = 1 To rows1 
        spID =  Worksheets("sheet1").Cells(i, "A").Value) 'the salepersonID
        total=0 'initializing counter
        for j= 1 to rows2
            spID2 =  Worksheets("sheet2").Cells(j, "A").Value 
            If (UserID=UserID2) Then
                total = total + Worksheets("sheet2").Cells(j, "B").Value
            End If
        next j
        Worksheets("sheet2").Cells(i, "B").Value = total ' Storing the total in sheet1
    next i

End Sub
于 2012-07-03T13:38:52.433 に答える