Results 1 to 20 of 29

Thread: Character Count

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    389
    Location
    Here's a function for counting characters, using the character and range object. Makes the code a bit smaller. It will work with a single character on multiple ranges, ignoring case, doesn't care what the character is. Will return an error if you use more then 1 character in the selection

    syntax
    PHP Code:
    =CountChar(Range,"character")
    =
    CountChar(A1:A5,"e"


    Function CountChar(R As Range, Char As String)
    Dim c As Characters, Found As Integer, count1 As Integer, R2 As Range
    If Len(Char) <> 1 Then
        CountChar = "#N/A"
        GoTo Error
    End If
    For Each R2 In R
        For count = 1 To R2.Characters.count
            If UCase(R2.Characters(count, 1).Text) = UCase(Char) Then
                Found = Found + 1
            End If
        Next count
    Next
    CountChar = Found
    Error:
    End Function


    edit:Thanks Dreamboat, It looks great this way.
    Last edited by CBrine; 06-22-2004 at 08:08 AM.
    The most difficult errors to resolve are the one's you know you didn't make.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •