Excel 中的多张条件总和

Conditional Sum in Excel with Multiple Sheets

我的数据是这样的,它有 3 列:Town Code、Ward Code 和该 Ward 的相应人口:

| Town  |  Ward | Population |
|  1000 | 10001 |     20     |
|  1000 | 10002 |     30     |
|  1000 | 10003 |     40     |
|  1234 | 12341 |     50     |
|  1234 | 12342 |     35     |

我无法在 vba 中编写代码来计算城镇下所有选区的人口总和(即城镇总人口)。
注意事项: -
我有一个巨大的数据集。
我有多个工作表,上面有相同类型的数据,具有不同的城镇 ID(每个城镇的病房数量也不同)。如果您能在百忙之中抽出时间,请多多指教。

这是您的解决方案

How to perform SumIf using VBA on an array in Excel

 Sub FasterThanSumifs()
    'FasterThanSumifs Concatenates the criteria values from columns A and B -
    'then uses simple IF formulas (plus 1 sort) to get the same result as a sumifs formula

    'Columns A & B contain the criteria ranges, column C is the range to sum
    'NOTE: The data is already sorted on columns A AND B

    'Concatenate the 2 values as 1 - can be used to concatenate any number of values
    With Range("D2:D25001")
        .FormulaR1C1 = "=RC[-3]&RC[-2]"
        .Value = .Value
    End With

    'If formula sums the range-to-sum where the values are the same
    With Range("E2:E25001")
        .FormulaR1C1 = "=IF(RC[-1]=R[-1]C[-1],RC[-2]+R[-1]C,RC[-2])"
        .Value = .Value
    End With

    'Sort the range of returned values to place the largest values above the lower ones
    Range("A1:E25001").Sort Key1:=Range("D1"), Order1:=xlAscending, _
    Key2:=Range("E1"), Order2:=xlDescending, Header:=xlYes
    Sheet1.Sort.SortFields.Clear

    'If formula returns the maximum value for each concatenated value match &
    'is therefore the equivalent of using a Sumifs formula
    With Range("F2:F25001")
        .FormulaR1C1 = "=IF(RC[-2]=R[-1]C[-2],R[-1]C,RC[-1])"
        .Value = .Value
    End With

    End Sub