簡體   English   中英

計算細胞和合並細胞的混合物

[英]Count mixture of cells and merged cells

我在計算合並單元格時遇到問題,因為我使用的代碼會計算每個單元格,包括那些合並在一起的單元格。 現在,我有了公式來計算包括合並在一起的所有單元格在內的所有單元格,例如,如果我將6個單元格合並在一起,我希望將其計為1,但將其計為6。是否有一種方法可以計算a單細胞和合並細胞的混合物? 感謝您的幫助 (:

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
ColorFunction = vResult
End Function

G'day,

您可以考慮使用字典來跟蹤已經計算過的任何合並區域的地址。 這意味着合並的區域可能會覆蓋多行,並且仍然只會被計數一次。 是一個描述字典對象用法的好網站。 您將必須添加“ Microsoft腳本運行時”作為參考。

嘗試這樣的事情:

Dim wks As Worksheet
Dim rng As Range
Dim c As Range
Dim i As Integer
Dim mergedAreaAddressBook As New Scripting.Dictionary

i = 0

Set wks = Worksheets("Sheet1")
Set rng = wks.Range("A1:H6")

'For each cell in the range
For Each c In rng

  'If the adress of the merged area is not on your dictionary
  If Not mergedAreaAddressBook.Exists(c.MergeArea.Address) Then

      'Increment the counter
      i = i + 1

      'Add the cell address to your address book
      mergedAreaAddressBook.Add c.MergeArea.Address, c.MergeArea.Address

  End If

Next c

對於看起來像這樣的范圍,當子結束時,i的值為34。

可能有一種更簡單的方法,但這就是我現在能想到的!

祝您解決方案好運,希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM