0

我正在尝试做一些我真的不知道该怎么做的事情。

我有 2 个电子表格 sheet1 和 sheet2

在表 1 上,我有
A 列一个数字列表 (SalespersonsID)
B 列是表 2 上 B 列的总和

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

在工作表 2 上,我有
A 列有 salespersonsID
列 B 有多少小部件在该交易中由销售人员出售
列 C 有 TypeofWidget
列 D 有位置

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

我不知道该怎么做,但我需要插入销售员使用 Sheet2 的 C 和 D 列以及工作表 1 的销售 ID 作为标准销售的小部件总数,并将其插入 Sheet1 的 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 回答