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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

860 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