电子表格excel表格如何将有相同内容的行合并在一起
1.excel表格如何将有相同内容的行合并在一起
星期几和上课班级都一样才合并,宏会不会用?打开工作表后按ALT+F11调出VBA窗口,窗口左边工程栏下有个“Microsoft Excel 对象”,选择它点鼠标右键在弹出菜单中选择插入>>;模块,下面会出现模块>>;模块1,双击模块1,把下面的代码复制到右边空白处,以后你在数据表中按住ALT+F8,在弹出窗口中选择宏"合并"后执行就可以了.代码如下:
Sub 合并()
fr = [a65536].End(xlUp).Row
Range("A1:D" & fr).Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range( _
"D2"), Order2:=xlAscending, Key3:=Range("B2"), Order3:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
For i = fr To 2 Step -1
If Cells(i, 1) = Cells(i - 1, 1) And Cells(i, 4) = Cells(i - 1, 4) Then
If Cells(i, 3) = Cells(i - 1, 3) Then
Cells(i - 1, 2) = Cells(i - 1, 2) & "/" & Cells(i, 2)
Rows(i & ":" & i).Delete
Else
Cells(i - 1, 2) = Cells(i - 1, 2) & "/" & Cells(i, 2)
Cells(i - 1, 3) = Cells(i - 1, 3) & "/" & Cells(i, 3)
Rows(i & ":" & i).Delete
End If
End If
Next i
End Sub
以上代码达到下面的效果:
(原数据)
星期几 节次 上课老师 上课班级
周二 第1,2节 史风华 06信息1班
周一 第1,2节 史风华 06信息1班
周一 第1,2节 龙安利 06信息2班
周一 第3,4节 史风华 06信息2班
周一 第3,4节 龙安利 06信息1班
周二 第3,4节 史风华 06信息1班
(执行后)
星期几 节次 上课老师 上课班级
周二 第1,2节/第3,4节 史风华 06信息1班
周一 第1,2节/第3,4节 史风华/龙安利 06信息1班
周一 第1,2节/第3,4节 龙安利/史风华 06信息2班
