繁体   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