Categorizing phone numbers according to pattern

hasanlianar

New Member
Joined
Jul 21, 2011
Messages
43
Hello,

I have a list of phone numbers and patterns as shown below. Is it possible to somehow match phone numbers with patters. For example, 2000000 matches with A000000 or 2342222 matches with ABCAAAA. I only can think of nested IF formulas, though that formula will be too long and complicated.
Could someone help with a better idea? Please note that there can be even more numbers, for example, instead of 2000000, there can be 3000000


phone numberspatterns
2000000AAAAAAA
2000001A000000
2000002A00000A
2000022A00000B
2000033AA00000
2002222AB00000
2003333AAA0000
2020000A0A0000
2020002A0A0A0A
2020202A0AAAAA
2022222ABABABA
2023333ABAAAAA
2030000ABBBBBB
2030303AAAAAA0
2032222AAAAAAB
2033333AA0A0A0
2034444AAB0000
2100000AABBBBB
2200000AABABAB
2200002A0B0000
2200003A0B0B0B
2200022A0BBBBB
2200033AB0B0B0
2202020ABA0000
2202222ABB0000
2203333ABBBBBA
2220000ABC0000
2220002ABCBCBC
2222000ABCCCCC
2222200AAA000A
2222220AAABBBB
2222222AAAA000
2222223AAAABBB
2222233AAAAA00
2222333AAAAABB
2223333A0000AA
2230000A0000BB
2232222A00AAAA
2232323A00BBBB
2233333AA0000A
2234444AA0000B
2300002AA000AA
2300003AA000BB
2300004AA0AAAA
2302222AA0BBBB
2303030AABAAAA
2303333AABCCCC
2304444A0BAAAA
2320000A0BCCCC
2320002A0A000A
2322222A0ABBBB
2323232AB0000A
2323333AB0000B
2324444AB0000C
2330000AB0AAAA
2330003AB0BBBB
2332222AB0CCCC
2333332ABCAAAA
2333333ABCBBBB
2334444ABCDDDD
2340000ABA000A
2342222ABABBBB
2343333ABACCCC
2343434ABB000B
2344444ABBAAAA
2345555ABBCCCC

<tbody>
</tbody>
 
According to post #3, this is how I understand it:

2 won't necessarily be A though. Just the first number in the phone number. So, if I've understood the op correctly then:

2500000 = AB00000
4200000 = AB00000
2323456 = ABABCDE
1212345 = ABABCDE

The first number (regardless of what it is) is A. If that number is repeated in the whole number, the repeats will also be A.
The next number that is different from the first is B, repeats of that number will also be B.
And it looks like 0 is 0 either way.
etc... I tried to make a UDF but I messed something up somewhere. =/
 
Last edited:
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
dreid1011, just when I thought I had it.

Wow ... I totally missed that, thanks. In that case, StephenCrump's solution is the best bet.
 
Upvote 0
Hasan, I actually don't think my formula does what you want it to do. If you look at dreid1011's post #11 and then my post #12, I think you'll find I misunderstood what you were trying to do. I think that StephenCrump's solution using helper cells (post #8) is the one you want.
 
Last edited:
Upvote 0
Edit: Upon further inspection... I have not fixed code as well as I thought. I will update when I have it working. For now... disregard the code.

I fixed the UDF I was working on, and now I believe it produces the desired results. If you are not opposed to UDF's, then give this code a try.

Code:
Public Function NPattern(Nstr As Long) As String
'counter for Larr() and Chr() count
Dim Lcnt As Integer, Ccnt As Integer, Qcnt As Integer
'loop counters
Dim i As Integer, j As Integer, k As Integer, l As Integer
'arrays
Dim Narr(0 To 6) As String, Parr(0 To 6) As String, Larr(0 To 6) As String
'length of string
Dim Slen As Integer
'Initialize Ccnt to 65 (Ascii code for letter A)
Ccnt = 65
'Fill Larr with letters A to G
For Lcnt = 0 To 6
    Larr(Lcnt) = Chr(Ccnt)
    Ccnt = Ccnt + 1
Next Lcnt
Lcnt = 0
Nstr = CStr(Nstr)
Slen = VBA.Len(Nstr) - 1
For i = 0 To Slen
    Narr(i) = Mid(Nstr, (i + 1), 1)
Next i
For j = 0 To Slen
    If j = 0 Then
        Parr(j) = Larr(Lcnt)
        Lcnt = Lcnt + 1
    Else
        If Narr(j) = "0" Then
            Parr(j) = "0"
        Else
            For k = 0 To j
                If Narr(j) = Narr(k) Then
                    Parr(j) = Parr(k)
                    Exit For
                Else
                    Parr(j) = Larr(Lcnt)
                End If
                If Parr(k) = Larr(Lcnt) Then
                    Qcnt = Qcnt + 1
                End If
            Next k
            If Qcnt > 0 Then
                Lcnt = Lcnt + 1
                Qcnt = 0
            End If
        End If
    End If
Next j
For l = 0 To Slen
    NPattern = NPattern & Parr(l)
Next l
End Function

To install a UDF, do the following:

Press Alt + F11 to open the VB editor
Click the Insert menu and select Module
Copy and paste the above code into the new module
Make sure you save the workbook as Macro Enabled so you do not lose the UDF upon closing the book.

This UDF may now be used like any other worksheet function. This UDF accepts your phone number as an argument and will produce the pattern string. With that, you can use it in a Lookup or Match function to find related prices.

Use the UDF like so:

=NPattern(A2) and copy down as needed.
 
Last edited:
Upvote 0
Ok, *fingers crossed* I believe I have it working now. Follow the instructions in post #15 to install a UDF and give this a try:

Code:
Public Function NPattern(Nstr As Long) As String
'counter for Larr() and Chr() count
Dim Lcnt As Integer, Ccnt As Integer, Qcnt As Integer
'loop counters
Dim i As Integer, j As Integer, k As Integer, l As Integer
'arrays
Dim Narr(0 To 6) As String, Parr(0 To 6) As String, Larr(0 To 6) As String
'string check
Dim Xstr As String
'length of string
Dim Slen As Integer
'Initialize Ccnt to 65 (Ascii code for letter A)
Ccnt = 65
'Fill Larr with letters A to G
For Lcnt = 0 To 6
    Larr(Lcnt) = Chr(Ccnt)
    Ccnt = Ccnt + 1
Next Lcnt
Lcnt = 0
Nstr = CStr(Nstr)
Slen = VBA.Len(Nstr) - 1
For i = 0 To Slen
    Narr(i) = Mid(Nstr, (i + 1), 1)
Next i
For j = 0 To Slen
    If j = 0 Then
        Parr(j) = Larr(Lcnt)
        Lcnt = Lcnt + 1
    Else
        Xstr = ""
        For k = 0 To j
            Xstr = Xstr & Parr(k)
            If InStr(Xstr, Larr(Lcnt)) Then
                Qcnt = Qcnt + 1
            End If
            If Narr(j) = Narr(k) Then
                Parr(j) = Parr(k)
                Exit For
            Else
                If Narr(j) = "0" Then
                    Parr(j) = "0"
                Else
                    Parr(j) = Larr(Lcnt)
                End If
            End If
        Next k
        If Qcnt > 0 Then
            Lcnt = Lcnt + 1
            Qcnt = 0
        End If
    End If
Next j
For l = 0 To Slen
    NPattern = NPattern & Parr(l)
Next l
End Function
 
Upvote 0
With some slight modifications, this one can handle strings longer than 10 characters, and text strings as well.

Code:
Public Function NPattern(Nstr As String) As String
'counter for Larr() and Chr() count
Dim Lcnt As Long, Ccnt As Long, Qcnt As Long
'loop counters
Dim i As Long, j As Long, k As Long, l As Long
'arrays
Dim Narr() As String, Parr() As String, Larr() As String
'string check
Dim Xstr As String
'length of string
Dim Slen As Integer
Slen = VBA.Len(Nstr) - 1
ReDim Narr(0 To Slen)
ReDim Parr(0 To Slen)
ReDim Larr(0 To Slen)
'Initialize Ccnt to 65 (Ascii code for letter A)
Ccnt = 65
'Fill Larr with letters A to G
For Lcnt = 0 To Slen
    Larr(Lcnt) = Chr(Ccnt)
    Ccnt = Ccnt + 1
Next Lcnt
Lcnt = 0
For i = 0 To Slen
    Narr(i) = Mid(Nstr, (i + 1), 1)
Next i
For j = 0 To Slen
    If j = 0 Then
        Parr(j) = Larr(Lcnt)
        Lcnt = Lcnt + 1
    Else
        Xstr = ""
        For k = 0 To j
            Xstr = Xstr & Parr(k)
            If InStr(Xstr, Larr(Lcnt)) Then
                Qcnt = Qcnt + 1
            End If
            If Narr(j) = Narr(k) Then
                Parr(j) = Parr(k)
                Exit For
            Else
                If Narr(j) = "0" Then
                    Parr(j) = "0"
                Else
                    Parr(j) = Larr(Lcnt)
                End If
            End If
        Next k
        If Qcnt > 0 Then
            Lcnt = Lcnt + 1
            Qcnt = 0
        End If
    End If
Next j
For l = 0 To Slen
    NPattern = NPattern & Parr(l)
Next l
End Function
 
Last edited:
Upvote 0
@dreid1011, I hope your gift to this person and all who follows — of time and knowledge — is truly appreciated. People would have to pay top dollar to get this kind of customized work done elsewhere. Very generous of you.
 
Upvote 0
@dreid1011, I hope your gift to this person and all who follows — of time and knowledge — is truly appreciated. People would have to pay top dollar to get this kind of customized work done elsewhere. Very generous of you.

Thank you Erik :) I'm just sitting here at work playing with Excel between tasks. And learning from others here as I go.
 
Upvote 0
Another way:

Code:
Function Pattern(sIn As String) As String

    Dim lMax As Long, i As Long, p As Long
    Dim sOut As String, s As String
    Const CHAR_0 = 48
    Const CHAR_A = 65
    
    If Len(sIn) Then
        lMax = CHAR_0
        For i = 1 To Len(sIn)
            s = Mid(sIn, i, 1)
            If s = "0" Then
                sOut = sOut & "0"
            Else
                p = InStr(sIn, s)
                If p = i Then
                    lMax = Application.Max(lMax + 1, CHAR_A)
                    sOut = sOut & Chr(lMax)
                Else
                    sOut = sOut & Mid(sOut, p, 1)
                End If
            End If
        Next i
    End If
    
    Pattern = sOut

End Function
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,906
Members
449,132
Latest member
Rosie14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top