+ Reply to Thread
Results 1 to 15 of 15

Search for 3 digit number in a four digit number

Hybrid View

  1. #1
    Forum Contributor Jerry HKA's Avatar
    Join Date
    09-25-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    126

    Search for 3 digit number in a four digit number

    Hi everyone,

    Can anyone please create a VBA code to search a 3 digit number in many four digit numbers and count how many that are there? Let me explain more descriptively, in my worksheet, there are many 4 digit numbers, and I want to know how much times a certain 3 digit number appeared in my worksheet. In 1234, there is, 123,124,134,234, right? Okay, so if I want to search for like a 234 in my worksheet, the computer should recognize 1234, or 4321 or 4123 or any number that have 234(in any order like 432,342, and so on) and count how many are there. Please help. Here is one small part of my worksheet.
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search for 3 digit number in a four digit number

    Based on an User Defined Function I created for an OP in this thread, I created a slight variation for you.

    Option Explicit
    
    Function CountPER(NumRANGE As Range, ParamArray Args() As Variant) As Long
    'Count the cells that INCLUDE a permutation of the numbers listed in the ParamArray
    'there may be other numbers in the cell and the permutation is still recognized
    If NumRANGE Is Nothing Then Exit Function
    
    Dim i As Long, Num As Range, buf As String
    
        For Each Num In NumRANGE
            If Len(Num) - 1 >= UBound(Args) Then
                buf = Num
                For i = LBound(Args) To UBound(Args)
                    If InStr(buf, Args(i)) Then
                        buf = WorksheetFunction.Substitute(buf, Args(i), "", 1)
                    Else
                        GoTo NextNum
                    End If
                Next i
                CountPER = CountPER + 1
            End If
    NextNum:
        Next Num
        
    End Function

    This is a UDF, you install it in a standard code module, then you use it in a cell like any other function. Here is the formula how you would put it in M12:

    =CountPER($A$1:$J$6, 4,3,4)

    The first parameter is the range of cells to search.
    The second parameter is a list of numbers that make up your permutations. They can be in any order, as many as you wish.

    The function will return a count of how many cells contain ALL the numbers you've listed.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor Jerry HKA's Avatar
    Join Date
    09-25-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    126

    Re: Search for 3 digit number in a four digit number

    Hi Jbeaucaire,

    I can't seem to make it work, can you please apply it to the attached file in post 1, and attached it for me, please? Thank you.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for 3 digit number in a four digit number

    Don't understand why "4054" in Yellow counts for "434".
    Option Explicit
    
    Sub test()
        Dim myNumbers, e, i As Long, x, y
        myNumbers = VBA.Array(VBA.Array(434, 0), VBA.Array(663, 0))
        With Cells(1).CurrentRegion
            For i = 0 To UBound(myNumbers)
                x = mySort(myNumbers(i)(0))
                If IsArray(x) Then
                    For Each e In .Value
                        y = mySort(e)
                        If IsArray(y) Then
                            If Join$(y, "") Like "*" & Join$(x, "*") & "*" Then
                                myNumbers(i)(1) = myNumbers(i)(1) + 1
                            End If
                        End If
                    Next
                End If
            Next
            With .Offset(, .Columns.Count + 2).Resize(1, 2)
                .CurrentRegion.ClearContents
                .Value = [{"Numbwer","Appearance"}]
                .Offset(1).Resize(UBound(myNumbers) + 1).Value = _
                Application.Transpose(Application.Transpose(myNumbers))
            End With
        End With
    End Sub
    
    Private Function mySort(txt)
        Dim a, i As Long, ii As Long, temp
        If Len(txt) > 1 Then
            a = Split(StrConv(txt, 64), Chr(0))
            ReDim Preserve a(UBound(a) - 1)
            For i = LBound(a) To UBound(a) - 1
                For ii = i + 1 To UBound(a)
                    If a(i) > a(ii) Then
                        temp = a(i)
                        a(i) = a(ii)
                        a(ii) = temp
                    End If
                Next
            Next
            mySort = a
        ElseIf Len(txt) = 1 Then
            mySort = VBA.Array(txt)
        Else
            mySort = Empty
        End If
    End Function
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Search for 3 digit number in a four digit number

    Option Explicit
    Sub Tim3So()
     Dim Jj As Byte, Num As Integer, Tr As Byte, Ch As Byte, DV As Byte
     ReDim Arr(1 To 6)
     Dim Rng As Range, sRng As Range
     Dim MyAdd As String
     
     Num = InputBox("ENTER xyz < 433", "GPE.COM", 432)
     If Num > 433 Or Num < 123 Then Exit Sub
     Tr = Num \ 100
     Ch = (Num \ 10) Mod 10
     DV = Num Mod 10
     Arr(1) = Num
     Arr(2) = Ch * 100 + Tr * 10 + DV
     Arr(3) = Ch * 100 + Tr + DV * 10
     Arr(4) = DV * 100 + Tr * 10 + Ch
     Arr(5) = DV * 100 + Ch * 10 + Tr
     Arr(6) = Tr * 100 + DV * 10 + Ch
     Set Rng = ActiveSheet.UsedRange
     Rng.Interior.ColorIndex = 0
     For Jj = 1 To 6
        Set sRng = Rng.Find(Arr(Jj), , xlFormulas, xlPart)
        If Not sRng Is Nothing Then
            MyAdd = sRng.Address
            Do
                sRng.Interior.ColorIndex = 34 + Jj
                Set sRng = Rng.FindNext(sRng)
            Loop While Not sRng Is Nothing And sRng.Address <> MyAdd
        End If
     Next Jj
    End Sub

  6. #6
    Forum Contributor Jerry HKA's Avatar
    Join Date
    09-25-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    126

    Re: Search for 3 digit number in a four digit number

    Sorry, it was my mistake that the 4504 was highlighted. It shouldn't be.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for 3 digit number in a four digit number

    Quote Originally Posted by Jerry HKA View Post
    Sorry, it was my mistake that the 4504 was highlighted. It shouldn't be.
    Is it working or not?

  8. #8
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Search for 3 digit number in a four digit number

    Quote Originally Posted by Jerry HKA View Post
    Hi everyone,

    Can anyone please create a VBA code to search a 3 digit number in many four digit numbers and count how many that are there? Let me explain more descriptively, in my worksheet, there are many 4 digit numbers, and I want to know how much times a certain 3 digit number appeared in my worksheet. In 1234, there is, 123,124,134,234, right? Okay, so if I want to search for like a 234 in my worksheet, the computer should recognize 1234, or 4321 or 4123 or any number that have 234(in any order like 432,342, and so on) and count how many are there. Please help. Here is one small part of my worksheet.
    Option Explicit
    Sub Tim3So()
     Dim Jj As Byte, Num As Integer, Tr As Byte, Ch As Byte, DV As Byte
     ReDim Arr(1 To 6)
     Dim Rng As Range, sRng As Range
     Dim MyAdd As String
     
     Num = InputBox("ENTER xyz < 433", "GPE.COM", 432)
     If Num > 433 Or Num < 123 Then Exit Sub
     Tr = Num \ 100
     Ch = (Num \ 10) Mod 10
     DV = Num Mod 10
     Arr(1) = Num
     Arr(2) = Ch * 100 + Tr * 10 + DV
     Arr(3) = Ch * 100 + Tr + DV * 10
     Arr(4) = DV * 100 + Tr * 10 + Ch
     Arr(5) = DV * 100 + Ch * 10 + Tr
     Arr(6) = Tr * 100 + DV * 10 + Ch
     Set Rng = ActiveSheet.UsedRange
     Rng.Interior.ColorIndex = 0
     For Jj = 1 To 6
        Set sRng = Rng.Find(Arr(Jj), , xlFormulas, xlPart)
        If Not sRng Is Nothing Then
            MyAdd = sRng.Address
            Do
                sRng.Interior.ColorIndex = 34 + Jj
                Set sRng = Rng.FindNext(sRng)
            Loop While Not sRng Is Nothing And sRng.Address <> MyAdd
        End If
     Next Jj
    End Sub

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search for 3 digit number in a four digit number

    Here is your workbook back with the UDF from post #2 already installed.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Search for 3 digit number in a four digit number


  11. #11
    Forum Contributor Jerry HKA's Avatar
    Join Date
    09-25-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    126

    Re: Search for 3 digit number in a four digit number

    To Sa DQ,
    I can't seem to make it work. Can you apply it to the attached in post 1. Thank you.

    To Jindon,
    Why is it only searching for 434 and 663, if it is not the result you got, can you approach the code and make it to search the worksheet, and arrange the numbers from most appeared, to least appeared. Thank you.

    To Jbeaucaire,
    Can you make the code help me search for many numbers at once, that will help me save time, pleas do try. Thank you

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for 3 digit number in a four digit number

    Quote Originally Posted by Jerry HKA View Post
    To Jindon,
    Why is it only searching for 434 and 663, if it is not the result you got, can you approach the code and make it to search the worksheet, and arrange the numbers from most appeared, to least appeared. Thank you.
    I don't understand what you are talking about.
    I wrote the code according to your sample file.

    What is your question then???

  13. #13
    Forum Contributor Jerry HKA's Avatar
    Join Date
    09-25-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    126

    Re: Search for 3 digit number in a four digit number

    I'm terribly sorry, Jindon,

    Your code is only searching for 434 and 663, but I don't only want to find those numbers, there are million of numbers wanted from me. But I only gave the two numbers as an example of the desired result. Please disregards the other written in the post for you.

    Or is it my fault of not knowing how to use your code properly, if so, please educate me of what to do.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for 3 digit number in a four digit number

    Quote Originally Posted by Jerry HKA View Post
    I'm terribly sorry, Jindon,

    Your code is only searching for 434 and 663, but I don't only want to find those numbers, there are million of numbers wanted from me. But I only gave the two numbers as an example of the desired result. Please disregards the other written in the post for you.
    Where are they?

  15. #15
    Forum Contributor Jerry HKA's Avatar
    Join Date
    09-25-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    126

    Re: Search for 3 digit number in a four digit number

    Okay, I tanhk you Jindon for helping me out here. I will be needing your help in the future. Thank you, Jbeaucaire, for your help. I really respect you. Thank you for all.

+ 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