角的度量(一)教学设计:自动查找合并单元格并将其解除合并、填充原合并单元格数字

来源:百度文库 编辑:偶看新闻 时间:2024/04/30 10:43:10
Excel:自动查找合并单元格并将其解除合并、填充原合并单元格数字2009-06-26 10:45

Private Sub CommandButton1_Click()
Dim i As Range
For Each i In ActiveSheet.UsedRange
If i.Address <> i.MergeArea.Address And i.Address = i.MergeArea.Item(1).Address Then
i.MergeArea.Select
i.MergeArea.UnMerge
' MsgBox (i.MergeArea.Address)
Selection.FillDown
End If
Next i
End Sub

或:

Private Sub CommandButton1_Click()
Dim i, j, n As Integer
For i = 1 To Range("A1").End(xlDown).Row
If Cells(i, 2).Address <> Cells(i, 2).MergeArea.Address Then

n = Cells(i, 2).MergeArea.Rows.Count ' 统计合并单元格行数
For j = 1 To 5 ' 有5列单元格需解除合并
Cells(i, 1 + j).MergeArea.UnMerge
For k = 1 To n - 1
Cells(i, j + 1) = Cells(i, j + 1) + Cells(i + k, j + 1) ' 将解除合并的单元格(或原本就未合并的单元格)的数据合并

Next k

Next j

For k = 1 To n - 1
Cells(i + 1, 2).EntireRow.Delete ' 删除多余行
Next k
End If
Next i
End Sub