【问题标题】:Count merged cells that have substring计算具有子字符串的合并单元格
【发布时间】:2020-11-06 18:42:32
【问题描述】:

我有一些由前同事编写的代码,用于计算具有特定字符串的合并单元格。例如,如果有一个大小为 3 且名称为“Youtube”的合并单元格,它将返回 3。

这是有问题的函数:

Function MergedCellsCount(rRange As Range, crit As Variant) As Double
Application.Volatile
MergedCellsCount = 0 'in case there are no matches
    
For Each c In rRange
    If LCase(c.Value) = LCase(crit) Then
        MergedCellsCount = MergedCellsCount + c.MergeArea.Cells.Count
        prev_rng = c.MergeArea.Address
    End If
Next
 
' MergedCellsCount = MergedCellsCount / 5
End Function

但是现在,我想计算在该字符串中具有子字符串的单元格。 例如,如果有一个大小为 3 的合并单元格与“Youtube | Spotify”,我想知道如何更改该函数以搜索子字符串“Youtube”。

关于如何实现这一点的任何建议?

提前致谢!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    计算包含字符串的单元格(合并单元格)(UDF)

    • 请注意,如果您合并或取消合并条件范围内的单元格,则在下一次重新计算工作表之前,单元格计数不会更新。
    • 要触发重新计算,一个不错的“技巧”是通过双击列标题的右边框来自动调整列(例如,对于列 AAB 之间的短线)。李>

    守则

    Option Explicit
    
    Function MergedCellsCount(CriteriaRange As Range, _
                              Criteria As String) _
             As Long
        Application.Volatile
        Dim c As Range
        For Each c In CriteriaRange.Cells
            If Not IsError(c) Then
                If InStr(1, c.Value, Criteria, vbTextCompare) > 0 Then
                    MergedCellsCount = MergedCellsCount + c.MergeArea.Cells.Count
                End If
            End If
        Next
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2015-11-28
      • 2014-09-02
      • 2017-02-25
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      相关资源
      最近更新 更多