vbaexcel

Check for consecutive characters in an excel cell


If you could help me I am in need to finding out if a character of the alphabet repeats consecutively 3 or more times in a cell, eg if a cell is "aronfff" or "aaaaaron" I want it to return true otherwise to return false eg "aaron".

Function InRowChars(cell As String) As Boolean
Dim repeats As Integer, char As String, i As Integer
repeats = 0
char = "abcdefghijklmnopqrstuvwxyz"

For i = 1 To Len(cell)
    If cell.Value = " " Then
        repeats = chars + 1
    Else
        chars = 0
    End If
Next i

If chars = 3 Then
    InRowChars = True
Else
    InRowChars = False
End If

End Function

I don't know how to get the value of the cell to be checked against the alphabet.


Solution

  • Here is a another regex solution that returns TRUE or FALSE depending on whether or not there are three or more repeating alphabetic characters:

    Option Explicit
    Function TripleChars(S As String) As Boolean
        Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    With RE
        .Global = True
        .Pattern = "([a-z])\1\1"
        .ignorecase = True 'edit as desired
        TripleChars = .test(S)
    End With
    End Function
    

    And here is an explanation of the Regex Pattern:

    ([a-z])\1\1

    ([a-z])\1\1
    

    Options: Case insensitive; ^$ don’t match at line breaks

    Created with RegexBuddy