FANDOM


StrCountSubStrsEdit

Option Explicit
Function StrCountSubStrs(ByVal s As String, ByVal u As String) As Integer
    Dim i As Integer
    Dim Start As Integer
    Dim Count As Integer
   
    Start = 1
   
    Do
        i = InStr(Start, s, u)
        If i > 0 Then
            Count = Count + 1
            Start = i + Len(u)
        Else
            Start = Len(s) + 1
        End If
    Loop While Start <= Len(s)
   
    StrCountSubStrs = Count
   
End Function

Sub StrCountSubStrsTest()
    If StrCountSubStrs("Kenny", "n") <> 2 Then Stop Else Debug.Print "Pass"
    If StrCountSubStrs("Kennn", "n") <> 3 Then Stop Else Debug.Print "Pass"
    If StrCountSubStrs("nennn", "n") <> 4 Then Stop Else Debug.Print "Pass"
    If StrCountSubStrs("nennn", "nn") <> 1 Then Stop Else Debug.Print "Pass"
    If StrCountSubStrs("this is a test", "is") <> 2 Then Stop Else Debug.Print "Pass"
    If StrCountSubStrs("is this is a test", "is") <> 3 Then Stop Else Debug.Print "Pass"
    If StrCountSubStrs("is this is a test is", "is") <> 4 Then Stop Else Debug.Print "Pass"
    Debug.Print "all pass"
End Sub

FixPhoneEdit

Option Explicit

Function CountDigits(ByVal x As String, pureDigits As String) As Long
    Dim i As Long
    Dim s As String
    Dim a As Integer
    Dim c As Integer
    
    For i = 1 To Len(x)
        s = Mid(x, i, 1)
        a = Asc(s)
        
        If a >= Asc("0") And a <= Asc("9") Then
            CountDigits = s
            pureDigits = pureDigits + s
            c = c + 1
        End If
    Next
    
    CountDigits = c

End Function

Function FixPhone(phone) As String

    Dim x As String
    Dim digits As Long
    Dim pureDigits As String
    
    x = phone
    
    digits = CountDigits(x, pureDigits)
    
    If Len(pureDigits) > 0 Then
        If Left(pureDigits, 1) = "1" Then
            If digits = 11 Then
                digits = digits - 1
                pureDigits = Right(pureDigits, 10)
            End If
        End If
    End If
    
    If digits = 10 Then
        FixPhone = "+1 " & Left(pureDigits, 3) & "-" & Mid(pureDigits, 4, 3) & "-" & Right(pureDigits, 4)
    End If
    
End Function

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.