Solved

Excel 2010 phone number prefix formula macro

Posted on 2011-02-14
8
794 Views
Last Modified: 2012-05-11
I thought I was pretty good with excel formulas until someone asked me for this one!

I have a list of phone numbers in a spreadsheet. They are all in number only format: xxxxxxxxxx. No parentheses, dashes, spaces, etc.

What I need to do is put a prefix with the number “1” for every line EXCLUDING some specific area codes. For example:

661726XXXX
707651XXXX
714572XXXX
808432XXXX
818719XXXX
909427XXXX
954932XXXX
305932XXXX
561353XXXX
786353XXXX

Only the first six rows should have a 1 prefix. When I am done, it needs to look like this:

1661726XXXX
1707651XXXX
1714572XXXX
1808432XXXX
1818719XXXX
1909427XXXX
954932XXXX
305932XXXX
561353XXXX
786353XXXX

Obviously the following area codes are local and therefore do not require a 1 by the auto dialer: 954, 305, 561, 786. More may be added later, so I also need a way to modify the exclusion list.

Yes, I know I could easily sort the list and run a concatenate formula, but the list is huge and is updated daily. I need to make this a macro that can be run daily by an end user… someone absolutely oblivious to excel formulas… I would rather it be a macro than a VBA script, if possible.

BYW – this is not for spamming or debt collections, it’s strictly for “good” purposes!

Thanks in advance!!!
0
Comment
Question by:dan9591
  • 4
  • 3
8 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34893451
> What I need to do is put a prefix with the number “1” for every line EXCLUDING some specific area code

A sample showing your spreadsheet layout, with the excluded list will help us tailor an answer

Cheers

Dave
0
 

Author Comment

by:dan9591
ID: 34893471
Check out the before and after samples I posted in the question. The last 4 are excluded. They are: 305, 954, 561 and 786.
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 34893526
You can do it very quickly with arrays
http://www.experts-exchange.com/A_2684.html

This code assumes the exclusion list is in column A, numbers in column B

It will run for the entire used portion of column B, testing from A1 to the last used cell in column A for exclusions
hth

Dave



Sub PhoneCull()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim X
    Dim Y

    Dim lrow As Long
    Dim lrow2 As Long
    Set rng1 = Range([b1], Cells(Rows.Count, "b").End(xlUp))
    X = rng1
    Set rng2 = Range([a1], Cells(Rows.Count, "a").End(xlUp))
    Y = rng2
    For lrow = 1 To UBound(X)
        X(lrow, 1) = "1" & X(lrow, 1)
        For lrow2 = 1 To UBound(Y)
            If Mid$(X(lrow, 1), 2, 3) = Y(lrow2, 1) Then
                X(lrow, 1) = Right$(X(lrow, 1), Len(X(lrow, 1)) - 1)
                Exit For
            End If
        Next
    Next
    rng1 = X
End Sub

Open in new window

phone.xlsm
0
 
LVL 32

Expert Comment

by:aleghart
ID: 34893629
For consistency's sake, why do you omit the "1" for local numbers.  We dial all numbers with 1+ten-digit, or the PBX will add the 1 for us.  Even numbers in our own area code, which could for simplicity be dialed with just 7-digit.

If this is for an auto-dialer, then you're not saving any labor cost on dialing, but you're adding labor cost to process the list.  Plus, if you ever have to share the list with another call center or branch of your business, the numbers will be formatted incorrectly.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dan9591
ID: 34894003

brettdj-That’s a great article! I thought about an array, but the best I could do with it is replacing data. For instance replacing 707 with 1707. The problem was when there a 707 elsewhere in the phone number. 7072227072 turned into 170722217072... is there a way to run it on just the first three digits in a cell?

aleghart - That was the first question I asked! It seems their auto dialer system has a problem with the 1. They are working on this issue separately, but it may be a while until they get it resolved, so they decided to try to tackle the issue on 2 fronts.

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34894749
> is there a way to run it on just the first three digits in a cell?

Thats what my actual code does above :)

1) I add a 1 to all cells
2) Then take off the 1 if the 2 to 4 character string matches an exclsuion code

The article link was for background on the array/range read write

Cheers

Dave
0
 

Author Comment

by:dan9591
ID: 34896636
Dave- Sorry, I was responding on my blackberry and did not see the code. I tested it out this morning and it works perfectly. Brilliant! Thanks a lot!!!
0
 

Author Closing Comment

by:dan9591
ID: 34896643
Brilliant!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now