在处理Change
事件时,您需要考虑三种情况:
Target
仅由Unmerged
细胞组成
Target
仅包含一个Merged
范围
Target
由一个或多个Merged
范围加上零个或多个Unmerged
单元格组成
这些Range.MergeCells
属性揭示了这些可能性:
Range.MergeCells = FALSE
Range.MergeCells = TRUE
Range.MergeCells = NULL
当Range.MergeCells = NULL
您需要Target
单独检查范围内的每个单元格以查看合并的单元格时
像这样的东西
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cl As Range
If IsNull(Target.MergeCells) Then
'Changed Range contains one or more Merged cells and other cells
For Each cl In Target.Cells
If cl.MergeCells Then
' only consider top left cell of a merged range
If cl.Address = cl.MergeArea.Cells(1, 1).Address Then
If cl = "" Then
MsgBox "Merged Cell Deleted " & cl.MergeArea.Address
End If
End If
End If
Next
Else
If Target.MergeCells Then
' Changed Range is a single Merged cells
If Target.Cells(1, 1) = "" Then
MsgBox "Merged Cell Deleted " & Target.Address
End If
Else
'Changed Range is Unmerged cells only
End If
End If
End Sub