重复对一列中的 46 个单元格求和,按 scenario/year

Summing 46 cells in a column repeatedly, by scenario/year

我有以下区域经济数据,其中包含截至 2100 年各部门的估计产出,适用于各种增长情景。

我想对每个部门的价值求和以获得区域产出估计值。这需要每 46 个单元格重复聚合输出列,将结果放在另一列中(最好附加其他信息,例如 year/scenario ID 等)。

由于有超过 1,000,000 个细胞,这不是一项可以手动完成的工作。按情景、年份和地区对所有部门的区域产出求和的最简单方法是什么?

下面是一些示例数据:

scenario  year   region_id region_name  sector_id sector_name   value/output
1A_1C_1C    2000    1   London  2   Mining & quarrying      642.2357178
1A_1C_1C    2000    1   London  3   Food, drink & tobacco   1979.741211
1A_1C_1C    2000    1   London  4   Textiles etc            586.706604
1A_1C_1C    2000    1   London  5   Wood & paper            318.1535339
1A_1C_1C    2000    1   London  6   Printing & recording    1749.062012
...             
1A_1C_1C    2000    2   South East  1   Agriculture etc     771.9697876
1A_1C_1C    2000    2   South East  2   Mining & quarrying  542.2153931
1A_1C_1C    2000    2   South East  3   Food, drink & tob.  1990.234009
1A_1C_1C    2000    2   South East  4   Textiles etc        205.3581085

编辑:是否可以通过从第 7 列中获取每个区域的输出总和并将它们放在第 8 列中来报告结果。理想情况下,第 9 列将包含特定区域名称,第 10 列将包含具体场景变体,像这样:

 Output        Region        Scenario       Year
 XXXXXX        London        1A_1C_1C       2000
 XXXXXX        South East    1A_1C_1C       2000
 ...
 XXXXXX        London        1B_1C_1A       2000
 XXXXXX        South East    1B_1C_1A       2000
 ...

编辑:将 'year' 添加到输出示例

看来您实际上是在要求汇总 SQL SELECT 语句,也许应该这样处理。

Sub Sheets_Group_Having()
    'Dim cnx As ADODB.Connection, rs As ADODB.Recordset
    Dim cnx As Object, rs As Object
    Dim sWS As String, sWB As String, sCNX As String, sSQL As String
    Dim ws1TBLaddr As String

    sWB = ThisWorkbook.FullName
    sWS = "Source Data Worksheet Name HERE"   `<~~ set the worksheet with the source data
    ws1TBLaddr = WorkSheets(sWS).Cells(1, 1).CurrentRegion.Address(0, 0)
    sCNX = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" & sWB _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

    Set cnx = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    cnx.Open sCNX

    sSQL = "SELECT SUM(w1.[value/output]), w1.[region_name], w1.[scenario], w1.[year] FROM [" & sWS & "$" & ws1TBLaddr & "] w1"
    sSQL = sSQL & " GROUP BY w1.[region_id], w1.[region_name], w1.[scenario], w1.[year]"
    sSQL = sSQL & " ORDER BY w1.[region_id], w1.[scenario]"
    'Debug.Print sSQL

    rs.Open sSQL, cnx

    With Worksheets.Add(after:=Sheets(Sheets.Count))
        .Name = "Summary Data"
        .Range("A1").Resize(1, 4) = Array("output", "region", "scenario", "year")
        .Range("A2").CopyFromRecordset rs
    End With

    rs.Close: Set rs = Nothing
    cnx.Close: Set cnx = Nothing
End Sub

我在Excel中弄清楚了如何进行数值计算。公式如下:

 =SUM(OFFSET($E,(ROW()-2)*46,0,46,1))

这将使用 OFFSET 函数每 46 行向下求和 E2 中的所有值。诀窍在于 row() 中 returns 行号,从该数字中取 2(因为我从 C2 开始),然后将其乘以 46,将 SUM 函数向下移动该列。