+ Reply to Thread
Results 1 to 7 of 7

Counting number of specific number of xxx in each cell?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2010
    Location
    NC, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Counting number of specific number of xxx in each cell?

    Hello all, I had been looking online to find a solution to my problem and was lucky enough to stumble on this board. I had 2 questions someone on here can hopefully solve.

    When working with a group of cells (say 10x10) with a number in each cell, what is the easiest way to count the number of 3’s for each cell (ex: 23253.213 would be 3)?

    Secondly, how do I change each 3 to an 8 one at a time for a cell, like change3once = 28253.213, change3twice = 28258.213, etc?

    Thank you in advance

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Counting number of specific number of xxx in each cell?

    Your first question can be solved with

    =LEN(A1)-LEN(SUBSTITUTE(A1,3,))
    Martin

  3. #3
    Registered User
    Join Date
    06-01-2010
    Location
    NC, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Counting number of specific number of xxx in each cell?

    Quote Originally Posted by mrice View Post
    Your first question can be solved with

    =LEN(A1)-LEN(SUBSTITUTE(A1,3,))
    Martin; Thanks for your quick response. I see it works for an excel cell, but I am unable to get it to work in Visual Basic (editing my macro). Is there something different I need to do so it will work there?

  4. #4
    Registered User
    Join Date
    06-01-2010
    Location
    NC, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Counting number of specific number of xxx in each cell?

    Quote Originally Posted by blitz View Post
    Martin; Thanks for your quick response. I see it works for an excel cell, but I am unable to get it to work in Visual Basic (editing my macro). Is there something different I need to do so it will work there?
    Finally figured out for VBA code it would be
    NumberOfThrees=(Len(CellX) - Len(Application.Substitute(CellX, 3, ""))
    Now to figure out the other item
    Last edited by blitz; 06-02-2010 at 08:06 PM. Reason: code tag

  5. #5
    Registered User
    Join Date
    06-01-2010
    Location
    NC, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Counting number of specific number of xxx in each cell?

    BUMB: Still unable to figure this out

    Secondly, how do I change each 3 to an 8 one at a time for a cell, like change3once = 28253.213, change3twice = 28258.213, etc?

  6. #6
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Counting number of specific number of xxx in each cell?

    Please use code tags when editing VBA Thx

  7. #7
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Counting number of specific number of xxx in each cell?

    Try

    Sub Test()
    X = 23253.213
    Y = ChangeSpecial(X, 3, 8, 2)
    End Sub
    
    
    
    Function ChangeSpecial(ByVal MyNumber As Double, NumberToReplace As Integer, NumberToReplaceWith As Integer, Count As Long) As Double
    Dim N As Integer, M As Integer
    For N = 1 To Len(MyNumber)
        If Mid(MyNumber, N, 1) = NumberToReplace Then
            M = M + 1
            If M <= Count Then
                MyNumber = Left(MyNumber, N - 1) & NumberToReplaceWith & Right(MyNumber, Len(MyNumber) - N + 1)
            End If
        End If
    Next N
    ChangeSpecial = Val(MyNumber)
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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