您可以使用下面的函數。我曾評論它來解釋發生了什麼,但本質:
- 遍歷所有的細胞在給定範圍內
- 檢查細胞是在其
MergeArea
第一個單元格。請注意,如果未合併,MergeArea
只是單元格本身。
- 檢查是否爲空。
- 如果顯示爲空,則顯示調試語句,顯示單元的地址/
MergeArea
。
代碼:
Function EmptyTest(rng As Range) As Boolean
EmptyTest = False ' Set to true if any cell in rng is empty
Dim cell As Range
For Each cell In rng
' Add your custom check here, e.g. for cell colour is black test do
' If cell.Interior.Color = RGB(0,0,0) Then
' Check it is the first cell in MergeArea
If cell.Address = cell.MergeArea.Cells(1).Address Then
' Check if it's empty
If Len(cell.MergeArea.Cells(1).Value) = 0 Then
EmptyTest = True
' Have the immediate window open to see debug statements
Debug.Print "Range: " & cell.MergeArea.Address & " is empty."
End If
End If
' "End If" here if you added a custom check like the interior colour demo above.
Next cell
End Function
實施例:
在VBA編輯器,呼叫
EmptyTest Range("A1:G4")
輸出在即時窗口*:
Range: $A$1:$B$4 is empty.
Range: $C$3 is empty.
Range: $G$4 is empty.
這個功能也返回取決於任何細胞是否空True
或False
。
*即時窗口可以在VBA編輯器按Ctrl鍵 + ģ被打開。
你到目前爲止嘗試過什麼?你期望如何處理代碼?只要檢查一個範圍內的單元格是否合併? – BruceWayne
查看https://stackoverflow.com/questions/22075988/detect-merged-cells-in-vba-excel-with-mergearea –