Vba 宏组合一些行表中的数据

发布于 2025-01-11 12:07:22 字数 2447 浏览 0 评论 0原文

我需要帮助来制作一个宏,该宏组合了 Excel 表中某些行的数据。我的表格如下所示:

A headerheader 2header 3header 4
Apple213
Banana324
Mango435
Grape546
Lime657

我需要一个宏来识别“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 headerheader 2header 3header 4
Apple213
Banana324
Mango435
Grape546
Lime657

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2025-01-18 12:07:22

您可以使用 Excel 中的普通函数来实现:

在此处输入图像描述

我在总计行中的公式是:

=SUM(SUMIF($A$2:$A$6;{"Banana";"Mango";"Grape"};B$2:B$6))

拖动到右侧。请注意,这里的关键部分是字符串 {"Banana";"Mango";"Grape"},它将过滤您想要获取的总和。也许您可以在 VBA 中编写一些代码来操作该部分,并在每次运行它时选择您想要总结的内容。

You can get that with normal functions in Excel:

enter image description here

My formula in Total row is:

=SUM(SUMIF($A$2:$A$6;{"Banana";"Mango";"Grape"};B$2:B$6))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文