|
初学者,用excel的理解方式拆分的代码。
Sub hebing()
Set wb = ThisWorkbook
arr = wb.Sheets(1).UsedRange
rw = UBound(arr)
For i = 2 To rw
t = wb.Sheets(1).Cells(i, 1).End(xlDown).Row - 1
If t > rw Then
t = rw
End If
wb.Sheets(1).Range(Cells(i, 1), Cells(t, 1)).Select
Selection.Merge
For m = i + 1 To t
t1 = wb.Sheets(1).Cells(m, 2).End(xlDown).Row - 1
If t1 > t Then
t1 = t
End If
wb.Sheets(1).Range(Cells(m, 2), Cells(t1, 2)).Select
Selection.Merge
For m2 = m + 1 To t1
t2 = wb.Sheets(1).Cells(m2, 3).End(xlDown).Row - 1
If t2 > t1 Then
t2 = t1
End If
wb.Sheets(1).Range(Cells(m2, 3), Cells(t2, 3)).Select
Selection.Merge
For m3 = m2 + 1 To t2
t3 = wb.Sheets(1).Cells(m3, 4).End(xlDown).Row - 1
If t3 > t2 Then
t3 = t2
End If
wb.Sheets(1).Range(Cells(m3, 4), Cells(t3, 4)).Select
Selection.Merge
For m4 = m3 + 1 To t3
t4 = wb.Sheets(1).Cells(m4, 5).End(xlDown).Row - 1
If t4 > t3 Then
t4 = t3
End If
wb.Sheets(1).Range(Cells(m4, 5), Cells(t4, 5)).Select
Selection.Merge
m4 = t4
Next m4
m3 = t3
Next m3
m2 = t2
Next m2
m = t1
Next m
i = t
Next i
wb.Sheets(1).Range(Cells(2, 1), Cells(t, 6)).Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
End With
End Sub |
|