Excel 2010 phone number prefix formula macro

Posted on 2011-02-14
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:


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


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!!!
Question by:dan9591
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
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



Author Comment

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.
LVL 50

Accepted Solution

Dave Brett earned 500 total points
ID: 34893526
You can do it very quickly with arrays

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


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
    rng1 = X
End Sub

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 32

Expert Comment

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.

Author Comment

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.

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



Author Comment

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!!!

Author Closing Comment

ID: 34896643

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

738 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