+ Reply to Thread
Results 1 to 6 of 6

counting the number of occurences of a character in string

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-16-2004
    Location
    Devon UK
    MS-Off Ver
    2010
    Posts
    357

    counting the number of occurences of a character in string

    Can anyone help with a formula to count the number of times a particular character is found in a string I have been experimenting with the likes of
    count = instr(teststring,count(";"))
    but obviously this doesnt work, VBA help doesn't say much about the count function
    Last edited by tryer; 01-31-2009 at 03:56 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: counting the number of occurences of a character in string

    I wouldn't use Count as a variable name given it's a function... perhaps lngCount (Long), eg:

    Dim lngCount As Long
    Dim strTest As String
    strTest = "Hello;Hello;Hello"
    lngCount = Len(strTest) - Len(Replace(strTest,";",""))

  3. #3
    Valued Forum Contributor
    Join Date
    11-16-2004
    Location
    Devon UK
    MS-Off Ver
    2010
    Posts
    357

    Re: counting the number of occurences of a character in string

    Thanks DonkeyOte works perfectly

  4. #4
    Chris Bode
    Guest

    Re: counting the number of occurences of a character in string

    Probably this will help you out,
        Dim cnt As Integer
        Dim str As String
        str = "dsada,dsad,dasdsada,dsada,"
        Dim i As Integer
        cnt = 0
        For i = 1 To Len(str)
            If Mid(str, i, 1) = "," Then
                cnt = cnt + 1
            End If
        Next
     
        MsgBox cnt.
    Here cnt contains the number of occurance of character “,”…

    Hope you get it

    Have a nice time



    Chris
    Last edited by VBA Noob; 02-15-2009 at 06:38 AM.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: counting the number of occurences of a character in string

    Hello Tryer,

    Here is another method that doesn't change the original string. It also performs a non case sensitive search. It can be used to search for a single character or multiple characters.
    Sub Macro1()
     
      Dim Cnt As Long
      Dim S As String
      
        S = "ABDaCdEFIoPxAZZQa"
        Cnt = UBound(Split(S, "a", , vbTextCompare))
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Valued Forum Contributor
    Join Date
    11-16-2004
    Location
    Devon UK
    MS-Off Ver
    2010
    Posts
    357

    Re: counting the number of occurences of a character in string

    Thnaks guys, lots of different ways to achieve the same result

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1