使用宏计算每行每列的总计

时间:2013-05-07 03:54:58

标签: vba excel-vba subtotal excel

我需要VBA的一些帮助。我有这张表:

Game     Room     City          Machines_week_1     Played_week_1     Won_week_1     Machines_week_2     Played_week_2     Won_week_2
A        Julia   San Francisco     3                    1200            1100             2                   500              250
B        Julia   San Francisco     4                    200            100             1                   500              250
C        Julia   San Francisco     3                    500            100             5                   600              250
D        Julia   San Francisco     1                    200            50             2                   1000              750
B        Carla   San Francisco     1                    1000            600             2                   1500              500
C        Carla   San Francisco     3                    800            400             3                   500              250
E        Carla   San Francisco     6                    1200            1100             4                   600              300
A        Jaime   Rio de Janeiro     1                    200            50             2                   90              50
C        Jaime   Rio de Janeiro     3                    900            600             3                   500              250
(....)

我想计算每张城市和游戏的总数,忽略房间,在工作表的最后每周。类似的东西:

Game     Room     City          Machines_week_1     Played_week_1     Won_week_1     Machines_week_2     Played_week_2     Won_week_2
 (...)
A        Total   San Francisco     3                    1200            1100             2                   500              250
B        Total   San Francisco     5                    1200            700             3                   3000              750
C        Total   San Francisco     6                    1300            500             8                   1100              500
D        Total   San Francisco     1                    200            50             2                   1000              750
E        Total   San Francisco     6                    1200            1100             4                   600              300
A        Total   Rio de Janeiro     1                    200            50             2                   90              50
C        Total   Rio de Janeiro     3                    900            600             3                   500              250
(....)

我正在尝试这样的事情:

For p = 1 To LastRow

If Range("CA1").Offset(i, 0).Value <> 2 Then ( I have this much of columns with data and weeks)
a = Range("A1").Offset(p, 0).Value
c = Range("C1").Offset(p, 0).Value


For i = 1 To LastRow
    If Range("A1").Offset(i, 0).Value = a And Range("C1").Offset(i, 0).Value = c Then

    Machines1 = Range("D1").Offset(i, 0).Value + Machines1
    Played1 = Range("E1").Offset(i, 0).Value + Played1
    (....)
    Machines2 =  Range("G1").Offset(i, 0).Value + Machines2
    Played2 = Range("H1").Offset(i, 0).Value + Played2
    Range("CA1").Offset(i, 0).Value = 2 (this column is empty, Im using it for a test to see if I have calculated this combination of city and game already)
    End If

    Next i

       lastn = Range("A65536").End(xlUp).Row
        Range("A1").Offset(lastn, 0).Value = a
        Range("B1").Offset(lastn, 0).Value = "Total"
        Range("C1").Offset(lastn, 0).Value = c
        Range("D1").Offset(lastn, 0).Value = Machines1
        Range("E1").Offset(lastn, 0).Value = Played1
        (...)
        Range("G1").Offset(lastn, 0).Value = Machines2
        Range("H1").Offset(lastn, 0).Value = Played2
        (...)

        Machines1=0
        Played1=0
        Machines2=0
        Played2 = 0
        (....)
        End If

        Next p

但它不起作用。它仍在为每个房间重复游戏(总数是正确的,但它们出现的次数与城市中的房间一样多)。如果有人可以帮助我,我会非常感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

作为替代方法,您可以在Excel中使用PivotTable functionality

使用的数据:

enter image description here

数据透视表输出:

enter image description here