Solved

Excel 2010 phone number prefix formula macro

Posted on 2011-02-14
8
792 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dan9591
Comment Utility

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
Comment Utility
> 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
Comment Utility
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
Comment Utility
Brilliant!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

744 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

16 Experts available now in Live!

Get 1:1 Help Now