使用 VBA 代码识别所有合并的单元格VBA 1:识别并高亮显示所有合并的单元格
1. 按住 ALT + F11 键,打开 Microsoft Visual Basic for Applications 窗口。
2. 点击“插入”>“模块”,并将以下宏粘贴到模块窗口中。
Sub FindMergedcells()
'updateby Extendoffice
Dim x As Range
For Each x In ActiveSheet.UsedRange
If x.MergeCells Then
x.Interior.ColorIndex = 8
End If
Next
End Sub
3. 按 F5 键运行此宏。当前工作表中的所有合并单元格都将被识别并高亮显示,参见截图:
VBA 2:识别并列出所有合并的单元格
1. 按住 ALT + F11 键,打开 Microsoft Visual Basic for Applications 窗口。
2. 点击“插入”>“模块”,并将以下宏粘贴到模块窗口中。
Sub ListMergedcells()
'updateby Extendoffice
Dim x As Range
Dim sMsg As String
sMsg = ""
For Each x In ActiveSheet.UsedRange
If x.MergeCells Then
If sMsg = "" Then
sMsg = "Merged cells:" & vbCr
End If
sMsg = sMsg & Replace(x.Address, "$", "") & vbCr
End If
Next
If sMsg = "" Then
sMsg = "No merged cells."
End If
MsgBox sMsg
End Sub
3. 按 F5 键运行此宏,所有合并的单元格都会在一个弹出的对话框中列出。参见截图: