2017-08-10 91 views
0

我想檢查在給定範圍內具有某些屬性(例如特定顏色)的所有單元格是否都爲空。在包含合併單元格的單元格範圍內循環

問題是,在該範圍內,有一些合併的單元格,並且這些單元格的尺寸各不相同(所以,我不能只檢測它們並系統地解決它們)。

有沒有像 東西對於每個小區範圍([某些範圍])

,會給我只有一個格的合併單元格?

我試過了,它給了我標準單元格,這意味着合併的單元格不止一次地被計數。並且該單元格的內容僅分配給左上角的單元格(因此,它會檢測合併單元格中的空單元格)

我也考慮過偏移量函數,但我認爲它不起作用(例如黑細胞合併細胞): Black cells are merged

+0

你到目前爲止嘗試過什麼?你期望如何處理代碼?只要檢查一個範圍內的單元格是否合併? – BruceWayne

+0

查看https://stackoverflow.com/questions/22075988/detect-merged-cells-in-vba-excel-with-mergearea –

回答

1

您可以使用下面的函數。我曾評論它來解釋發生了什麼,但本質:

  • 遍歷所有的細胞在給定範圍內
  • 檢查細胞是在其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 

實施例:

sheet

在VBA編輯器,呼叫

EmptyTest Range("A1:G4") 

輸出在即時窗口*:

Range: $A$1:$B$4 is empty. 
Range: $C$3 is empty. 
Range: $G$4 is empty. 

這個功能也返回取決於任何細胞是否空TrueFalse


*即時窗口可以在VBA編輯器按Ctrl鍵 + ģ被打開。

相關問題