vba 如何在excel中对数据进行分组并计算组数据的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11149617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:34:53  来源:igfitidea点击:

how to group a data in excel and calculate the sum of group data

excelexcel-vbavba

提问by Amir

I want to implement some code in excel macro to group some data for me and then calculate the sum of grouped column. please look at below data.

我想在excel宏中实现一些代码来为我分组一些数据,然后计算分组列的总和。请看下面的数据。

I want to check instead of writing macro is there any way to implement it via excel formula? I mean via excel formula I group them and calculate the total time in another column?

我想检查而不是编写宏有没有办法通过excel公式实现它?我的意思是通过 excel 公式我将它们分组并计算另一列中的总时间?

Fix.No  Duration        Expected result 
111 00:00:55        Fix.No  Total Duration
111 00:59:15        111 01:02:25
123 00:10:55        123 00:10:55
111 00:01:15        265 00:00:45
265 00:00:45        214 01:00:05
214 01:00:05            

回答by kad81

If you're willing to manually (or via macro) enter the first column values (each distinct value as you have it there), then you can just use the SUMIF function to calculate the totals.

如果您愿意手动(或通过宏)输入第一列值(您在那里拥有的每个不同的值),那么您可以只使用 SUMIF 函数来计算总数。

回答by Joel Spolsky

You are looking for the PIVOT TABLE feature.

您正在寻找数据透视表功能

回答by user2905919

To get data similar to how SQL gives the output can be done in excel using the following steps

要获得类似于 SQL 如何给出输出的数据,可以使用以下步骤在 excel 中完成

  1. Select the source data in pivot
  2. Arrange all the non aggregate columns in row filter in pivot
  3. Select the aggregate columns in values in pivot
  4. Pivottable options--->Design----->Report Layout------>Show in tabular form
  5. Then select Repeat all item labels from Report layout tab
  6. Remove all the Grand Total and Sub Total Hope this helps and makes modifications accordingly
  1. 选择数据透视中的源数据
  2. 在数据透视表中排列行过滤器中的所有非聚合列
  3. 在数据透视表中选择值中的聚合列
  4. 数据透视表选项--->设计----->报表布局------>以表格形式显示
  5. 然后从报告布局选项卡中选择重复所有项目标签
  6. 删除所有总计和小计希望这有助于并相应地进行修改