答案1
答案2
该宏应该可以工作:
Public Sub merger()
Application.DisplayAlerts = False
Dim wkb As Workbook
Dim wks As Worksheet
Set wkb = ThisWorkbook
Set wks = wkb.Sheets("Sheet1")
firstrow = 2
lastcolumn = "Z"
columnsortkey = "C"
columnmerge = 3
Columns("A:" & lastcolumn).Sort key1:=Range(columnsortkey & ":" & columnsortkey), Header:=xlYes
theEnd = False
thisrow = firstrow
found = False
Count = 0
While theEnd = False
firstCell = wks.Cells(thisrow, columnmerge)
nextCell = wks.Cells(thisrow + 1, columnmerge)
If firstCell = nextCell Then
If found = False Then
found = True
mergerfirst = thisrow
Count = Count + 1
Else
Count = Count + 1
End If
Else
If found = True Then
Range(Cells(mergerfirst, columnmerge), Cells(mergerfirst + Count, columnmerge)).Merge
found = False
Count = 0
End If
End If
thisrow = thisrow + 1
If firstCell = "" Then theEnd = True
Wend
Application.DisplayAlerts = True
End Sub
为了使其工作,你必须用ALT+打开 VBA/Macros F11,然后在本工作簿插入一个新模块并粘贴代码。