Excel 对具有子字符串的合并单元格进行计数

Excel 对具有子字符串的合并单元格进行计数,excel,vba,Excel,Vba,我有一个前同事的一些代码,用一个特定的字符串计算合并的单元格。例如,如果有一个大小为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

我有一个前同事的一些代码,用一个特定的字符串计算合并的单元格。例如,如果有一个大小为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”

对如何实现这一目标有何建议

提前谢谢

计数包含字符串的单元格(合并单元格)(UDF)
  • 请注意,如果合并或取消合并条件范围内的单元格,则在下次重新计算工作表之前,不会更新单元格计数
  • 要触发重新计算,一个很好的“技巧”是通过双击列标题的右边框来自动调整列(例如,对于列
    a
    a
    B
    之间的短线)
代码

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
计数包含字符串的单元格(合并单元格)(UDF)
  • 请注意,如果合并或取消合并条件范围内的单元格,则在下次重新计算工作表之前,不会更新单元格计数
  • 要触发重新计算,一个很好的“技巧”是通过双击列标题的右边框来自动调整列(例如,对于列
    a
    a
    B
    之间的短线)
代码

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