How to Count the Number of a Specific Character in a Cell with Excel VBA

前端 未结 6 821
忘掉有多难
忘掉有多难 2021-01-14 22:26

I have a number of items in cells that are separated by dashes. I\'m trying to normalize the database by splitting rows so that each row contains only one entry. How do you

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 22:51

    This code might be of your help .. you can also use it as a UDF... :)

    Function CountHypens(rng_Src As Range) As Long
    
    'A VARIANT FOR SPLITTING CELL CONTENTS
    Dim var As Variant
    
    On Error Resume Next
    var = Split(rng_Src.Value, "-", , vbTextCompare)
    
    If Err.Number <> 0 Then
        Debug.Print "This cell does not have any hyphens."
    Else
        CountHypens = UBound(var)
        End If
    Err.Clear: On Error GoTo 0
    
    End Function
    

提交回复
热议问题