###### EXCLE带表头的单个Sheet 内,相同列的数据行自动汇总,第11列数量相加
###### 宏代码如下:
Sub Merge same lines
Dim R1 As Integer,NumDelRows As Integer,sumNumCul As Integer
sumNumCul = 11
Application.CutCopyMode = False
With ActiveSheet
a = ActiveSheet.UsedRange.Rows.Count
If a > 2000 Then MsgBox '数据条数过多,合并速度会过长或者没响应,按确定退出'
If a > 2000 Then End
R1 = .Cells(a +1,1).End(x1Up).Row
Range(.Rows(1),.Rows(R1)).Select
Selection.Copy
End With
Sheets.Add
my_string = CStr(Hour(Now))) & CStr(Minute(Now())) & CStr(Second(Nov()))
ActiveSheet.Paste
With Selection
i = 2: NumDelRows = 0
While i <= R1: i1 = i + 1
While i1 <= R1: 相同 = True
For j = 1 to sumNumCul
MsgBox.Cell(i1, j) & " " & .Cell(i, j)
If j <> sumNumCul Then If .Cells(i1, j) <> .Cells(i1, j)
Then 相同 = False: Exit For
Next
If 相同 Then
StatusBar = "合并了第 " & i & " 行与第 " & i1 & " 行 "
.Cells(i,sumNumCul) = .Cells(i, sumNumCul) + .Cells(i1, sumNumCul)
.Rows(i1).Delete
R1 = R1 - 1: NumDelRows = NumDelRows + 1
Else: i1 = i1 + 1
StatusBar = " 第 " & i & " 行与第" & i1 & " 行不同 "
End If
Wend: i = i + 1
Wend: MsgBox " 共计合并了 " & NumDelRows & " 行 "
End With
End Sub
###### 运行后提示Compile error, Arugment no optinal
初学VBA,请指正.
###### 运行宏,自动汇总列数较多但数据相同的行
拼写错误多
Sub Mergesamelines()
Dim R1 As Integer, NumDelRows As Integer, sumNumCul As Integer
sumNumCul = 11
Application.CutCopyMode = False
With ActiveSheet
a = ActiveSheet.UsedRange.Rows.Count
If a > 2000 Then MsgBox "数据条数过多,合并速度会过长或者没响应,按确定退出"
If a > 2000 Then End
R1 = .Cells(a + 1, 1).End(xlUp).Row
Range(.Rows(1), .Rows(R1)).Select
Selection.Copy
End With
Sheets.Add
my_string = CStr(Hour(Now)) & CStr(Minute(Now())) & CStr(Second(Now()))
ActiveSheet.Paste
With ActiveSheet
i = 2
NumDelRows = 0
While i <= R1
i1 = i + 1
While i1 <= R1
相同 = True
For j = 1 To sumNumCul
MsgBox .Cells(i1, j).Value & " " & .Cells(i, j).Value
If j <> sumNumCul Then If .Cells(i1, j).Value <> .Cells(i1, j).Value Then 相同 = False
Exit For
Next
If 相同 Then
StatusBar = "合并了第 " & i & " 行与第 " & i1 & " 行 "
.Cells(i, sumNumCul) = .Cells(i, sumNumCul) + .Cells(i1, sumNumCul)
.Rows(i1).Delete
R1 = R1 - 1
NumDelRows = NumDelRows + 1
Else
i1 = i1 + 1
StatusBar = " 第 " & i & " 行与第" & i1 & " 行不同 "
End If
Wend
i = i + 1
Wend
MsgBox " 共计合并了 " & NumDelRows & " 行 "
End With
End Sub