Vba 宏组合一些行表中的数据
我需要帮助来制作一个宏,该宏组合了 Excel 表中某些行的数据。我的表格如下所示:
A header | header 2 | header 3 | header 4 |
---|---|---|---|
Apple | 2 | 1 | 3 |
Banana | 3 | 2 | 4 |
Mango | 4 | 3 | 5 |
Grape | 5 | 4 | 6 |
Lime | 6 | 5 | 7 |
我需要一个宏来识别“Banana”、“Mango”的特定文本' 和 'Grape',然后将它们组合成一行,同时添加其他三列中的数据。因此,组合这些列应该如下所示。
| Total | 12 (3 + 4 + 5) | 9 (2+3+4) | 15 (4 + 5+ 6)|
任何帮助将不胜感激。提前致谢!
编辑:我应该补充一点,该表是由另一个宏使用另一张表中的数据创建的。其代码如下:
Sub CopyUnique()
Application.Calculation = xlAutomatic
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Bericht1")
Set s2 = Sheets("Sheet1")
s1.Range("E:E").Copy s2.Range("a1")
s2.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
s2.Range("B3").Select
ActiveCell.Formula2R1C1 = "=SUMIF('" & s1.Name & "'!R3C5:R50000C5,'" & s2.Name & "'!RC1,'" & s1.Name & "'!R3C20:R50000C20)"
s2.Range("B3:B" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
s2.Range("c3").Select
ActiveCell.Formula2R1C1 = "=SUMIF('" & s1.Name & "'!R3C5:R50000C5,'" & s2.Name & "'!RC1,'" & s1.Name & "'!R3C17:R50000C18)"
s2.Range("c3:c" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
s2.Range("d3").Select
ActiveCell.Formula2R1C1 = "=SUMPRODUCT(IF('" & s1.Name & "'!R3C5:R50000C5='" & s2.Name & "'!RC1, '" & s1.Name & "'! R3C20:R50000C20*'" & s1.Name & "'!R3C28:R50000C28))"
s2.Range("d3:d" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
s2.Range(Range("B3:D3"), Range("B3").End(xlDown)).Select
Selection.Copy
s2.Range("B3").PasteSpecial xlPasteValues
Range("A2").Select
Application.CutCopyMode = True
Dim r As Long, c As Long
Range("A2:D" & Rows.Count).SpecialCells(xlCellTypeConstants).BorderAround ColorIndex:=1, Weight:=xlThick
For c = 6 To 11
r = WorksheetFunction.Max(r, Cells(Rows.Count, c).End(xlUp).Row)
Next
Range("A2:D2").Resize(r).BorderAround ColorIndex:=11, Weight:=xlThick
Range("B2").Copy
Range("A2").PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub
I need help to make a macro which combines data of some rows in an excel table. My table looks like following:
A header | header 2 | header 3 | header 4 |
---|---|---|---|
Apple | 2 | 1 | 3 |
Banana | 3 | 2 | 4 |
Mango | 4 | 3 | 5 |
Grape | 5 | 4 | 6 |
Lime | 6 | 5 | 7 |
I need a macro to identify the specific texts of 'Banana', 'Mango' and 'Grape' and then combine these into one row, also adding the data in the other three columns. So combining these columns should look like this.
| Total | 12 (3 + 4 + 5) | 9 (2+3+4) | 15 (4 + 5+ 6)|
Any help would be greatly appreciated. Thanks in advance!
EDIT: I should add that this table is made by another macro using data from another sheet. The code for this is as follows:
Sub CopyUnique()
Application.Calculation = xlAutomatic
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Bericht1")
Set s2 = Sheets("Sheet1")
s1.Range("E:E").Copy s2.Range("a1")
s2.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
s2.Range("B3").Select
ActiveCell.Formula2R1C1 = "=SUMIF('" & s1.Name & "'!R3C5:R50000C5,'" & s2.Name & "'!RC1,'" & s1.Name & "'!R3C20:R50000C20)"
s2.Range("B3:B" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
s2.Range("c3").Select
ActiveCell.Formula2R1C1 = "=SUMIF('" & s1.Name & "'!R3C5:R50000C5,'" & s2.Name & "'!RC1,'" & s1.Name & "'!R3C17:R50000C18)"
s2.Range("c3:c" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
s2.Range("d3").Select
ActiveCell.Formula2R1C1 = "=SUMPRODUCT(IF('" & s1.Name & "'!R3C5:R50000C5='" & s2.Name & "'!RC1, '" & s1.Name & "'! R3C20:R50000C20*'" & s1.Name & "'!R3C28:R50000C28))"
s2.Range("d3:d" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
s2.Range(Range("B3:D3"), Range("B3").End(xlDown)).Select
Selection.Copy
s2.Range("B3").PasteSpecial xlPasteValues
Range("A2").Select
Application.CutCopyMode = True
Dim r As Long, c As Long
Range("A2:D" & Rows.Count).SpecialCells(xlCellTypeConstants).BorderAround ColorIndex:=1, Weight:=xlThick
For c = 6 To 11
r = WorksheetFunction.Max(r, Cells(Rows.Count, c).End(xlUp).Row)
Next
Range("A2:D2").Resize(r).BorderAround ColorIndex:=11, Weight:=xlThick
Range("B2").Copy
Range("A2").PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Excel 中的普通函数来实现:
我在总计行中的公式是:
拖动到右侧。请注意,这里的关键部分是字符串
{"Banana";"Mango";"Grape"}
,它将过滤您想要获取的总和。也许您可以在 VBA 中编写一些代码来操作该部分,并在每次运行它时选择您想要总结的内容。You can get that with normal functions in Excel:
My formula in Total row is:
Drag to right. Notice the critical part here is the string
{"Banana";"Mango";"Grape"}
, which will filter the sum you want to get. Probably you can code something in VBA to manipulate that part and choose what you want to sum up everytime you run it.