Solved

Excel 2010 phone number prefix formula macro

Posted on 2011-02-14
8
796 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Consolidation 8 19
how to transfer all external connections from one workbook another workbook? 6 22
macro modification Column C 14 26
Msgbox tickler 10 23
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

775 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