Excel VBA:合并行和总和值(Excel VBA: Merge Rows and Sum Values)

编程入门 行业动态 更新时间:2024-10-21 15:26:29
Excel VBA:合并行和总和值(Excel VBA: Merge Rows and Sum Values)

我已经阅读了关于合并行和合并数据的所有其他问题。 我确实遇到了一个我认为对我有用的解决方案,但是当我运行宏时,实际上并没有对正确的列进行总结。 作为VBA的新手,我无法确定需要在宏中更改哪些内容才能在我的工作表中工作。

背景:我想使用一个宏,因为我每天都得到一个我必须操作的报告,以便它可以处理到我们的系统中。 我创建了一个VBA宏来为我做操作,但我已经意识到报告现在有重复的行具有不同的值。 下面是一个示例,其中最后一组数字需要加在一起。 (我的实际报告中的J列)

第1行:C3 = 1234,名称,C5 = ABC,C5Name,C4 = DEF,C4Name,21361

第2行:C3 = 1234,名称,C5 = ABC,C5Name,C4 = DEF,C4Name,132165

这是我找到的解决方案,但我需要知道要改变什么以与我实际需要总结的列相对应。

Sub Merge()

Dim ColumnsCount As Integer
Dim i As Integer

Range("A1").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Do While ActiveCell.Row <= ActiveSheet.UsedRange.Rows.Count
    If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
        For i = 1 To ColumnsCount - 1
            ActiveCell.Offset(0, i).Value = ActiveCell.Offset(0, i).Value +     ActiveCell.Offset(1, i).Value
        Next
        ActiveCell.Offset(1, 0).EntireRow.Delete shift:=xlShiftUp
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Loop

End Sub 
  
 

非常感谢任何和所有的帮助。 如果我需要提供更多信息,请告诉我。

安德烈〜

I have read just about every other question on here on merging rows and consolidating data. I did come across a solution I think will work for me, but when I ran the macro it didn't actually sum the right column. Being new to VBA, I'm having trouble figuring out what needs to change in the macro to work in my sheet.

Background: I want to use a macro because I get a report every day that I have to manipulate so that it can process into our system. I have created a VBA macro to do the manipulation for me, but I have realized that the report now has duplicate lines with different values. Below is an example with the last set of numbers needing to be added together. (Column J on my actual report)

i.e.

Row 1: C3=1234, Name, C5=ABC, C5Name, C4=DEF, C4Name, 21361

Row 2: C3=1234, Name, C5=ABC, C5Name, C4=DEF, C4Name, 132165

This is the solution I found, but I need to know what to change to correspond with the column I actually need summed up.

Sub Merge()

Dim ColumnsCount As Integer
Dim i As Integer

Range("A1").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Do While ActiveCell.Row <= ActiveSheet.UsedRange.Rows.Count
    If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
        For i = 1 To ColumnsCount - 1
            ActiveCell.Offset(0, i).Value = ActiveCell.Offset(0, i).Value +     ActiveCell.Offset(1, i).Value
        Next
        ActiveCell.Offset(1, 0).EntireRow.Delete shift:=xlShiftUp
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Loop

End Sub 
  
 

Any and all help is greatly appreciated. Please let me know if I need to provide additional information.

~Andrea

最满意答案

看你的桌子会更好。 你还没有解释得足够多。 这个答案与user1016274的答案没那么不同。 然后,列B , D和H第一个顺序上的代码通过比较相同的列,在添加其J列值时检查并删除重复项。

Sub Merge() Range("A1").Sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("D1"), Order2:=xlAscending, _ Key3:=Range("H1"), Order3:=xlAscending, Header:=xlYes 'I assume there are column headers. If not, use "Header:=xlNo" instead of "Header:=xlYes" Range("A2").Select 'I assume there are column headers. If not, use "Range("A1").Select" instead of "Range("A2").Select" Do While ActiveCell.Row <= ActiveSheet.UsedRange.Rows.Count If ActiveCell.Offset(0, 1).Value = ActiveCell.Offset(1, 1).Value And ActiveCell.Offset(0, 3).Value = ActiveCell.Offset(1, 3).Value And ActiveCell.Offset(0, 7).Value = ActiveCell.Offset(1, 7).Value Then ActiveCell.Offset(0, 9).Value = ActiveCell.Offset(0, 9).Value + ActiveCell.Offset(1, 9).Value ActiveCell.Offset(1, 0).EntireRow.Delete shift:=xlShiftUp Else ActiveCell.Offset(1, 0).Select End If Loop End Sub

It would have been better to see your table. You still have not explained enough. This answer is not so different from user1016274's answer. The code above first order by the columns B, D and H then checks and deletes the duplicates by the time adding up their J column values, by comparing same columns.

Sub Merge() Range("A1").Sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("D1"), Order2:=xlAscending, _ Key3:=Range("H1"), Order3:=xlAscending, Header:=xlYes 'I assume there are column headers. If not, use "Header:=xlNo" instead of "Header:=xlYes" Range("A2").Select 'I assume there are column headers. If not, use "Range("A1").Select" instead of "Range("A2").Select" Do While ActiveCell.Row <= ActiveSheet.UsedRange.Rows.Count If ActiveCell.Offset(0, 1).Value = ActiveCell.Offset(1, 1).Value And ActiveCell.Offset(0, 3).Value = ActiveCell.Offset(1, 3).Value And ActiveCell.Offset(0, 7).Value = ActiveCell.Offset(1, 7).Value Then ActiveCell.Offset(0, 9).Value = ActiveCell.Offset(0, 9).Value + ActiveCell.Offset(1, 9).Value ActiveCell.Offset(1, 0).EntireRow.Delete shift:=xlShiftUp Else ActiveCell.Offset(1, 0).Select End If Loop End Sub

更多推荐

本文发布于:2023-08-04 22:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   VBA   Excel   Merge   Values

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!