我已经阅读了关于合并行和合并数据的所有其他问题。 我确实遇到了一个我认为对我有用的解决方案,但是当我运行宏时,实际上并没有对正确的列进行总结。 作为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 SubAny 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 SubIt 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
更多推荐
发布评论