簡體   English   中英

Excel中具有多個工作表的條件總和

[英]Conditional Sum in Excel with Multiple Sheets

我的數據如下所示,它包含3列:城鎮代碼,區號和該區的相應人口:

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

我無法用vba編寫代碼,無法給我鎮下所有病房的人口總數(即鎮上的總人口)。
注意事項:-
我有一個龐大的數據集。
我有多張紙,上面有相同種類的數據,但城鎮ID不同(每個城鎮的病房數也不同)。 如果您可以在繁忙的工作日程中抽出一些時間,請提供幫助。

這是您的解決方案

如何在Excel中的數組上使用VBA執行SumIf

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM