Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Preparing Data for Import into Access.

Preparing Data for Import into Access

Written by Allen Wyatt (last updated July 13, 2022)
This tip applies to Excel 2007 and 2010


5

If you are a database programmer you may sometimes get Excel files that you have to "clean up" to put into Access. Two common problems are caused by Social Security Numbers and ZIP Codes. These are best stored as text in the database, and not as numbers as they often are in Excel. (In Excel the numbers may display properly because of cell formatting, and not because they are stored as text.)

Even when the range is formatted as text in Excel, complete with leading zeroes, Access more often than not converts these values to numbers. However, if the number is preceded with an apostrophe, as for a label, Access will correctly import it as text without the leading apostrophe.

To prepare Social Security Numbers for importing in Access a quick little macro can come in handy—one that makes sure that leading zeros are present and that the apostrophe is in place for the cell. To use the macro, just select the range of Social Security Numbers and then run the macro:

Sub SSN2Text()
    Dim c As Range
    Application.ScreenUpdating = False
    'Format selected cells as text
    Selection.NumberFormat = "@"
    For Each c In Selection
        If Left(c, 1) = "'" Then
            'strip the apostrophe, if any
            c = Mid(c, 2, 99)
        Else
            c = "'" & Right("000000000" & c, 9)
        End If
    Next c
    Application.ScreenUpdating = True
End Sub

The solution for the ZIP Codes is similar in nature. The macro to process ZIP Codes steps through each cell in the selection, formats it as text, adds a leading apostrophe, and plugs in any leading zeroes. The difference is that the macro must also account for instances where there are either five-digit or nine-digit ZIP Codes.

Sub ZIP2Text()
    Dim c As Range
    Application.ScreenUpdating = False
    'Format selected cells as text
    Selection.NumberFormat = "@"
    For Each c In Selection
        If Left(c, 1) = "'" Then
            'strip the apostrophe, if any
            c = Mid(c, 2, 99)
        End If
        If Len(c) <= 5 Then
            c = "'" & Right("00000" & c, 5)
        Else
            c = "'" & Right("00000" & c, 10)
        End If
    Next c
    Application.ScreenUpdating = True
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11228) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Preparing Data for Import into Access.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Overlining Characters

Want to add an overline above a character or two in your document? There are several ways you can try, as described in ...

Discover More

Converting UTC Times to Local Times

Dates and times are often standardized on UTC time, which is analogous to GMT times. How to convert such times to your ...

Discover More

Floating Footer

Need some specific text to appear just below the end of the text on the last page of your document? You can accomplish ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Can't Enter Years in a Cell

Sometimes getting the right thing to show up in a cell can be a bit tricky when working with dates. If you enter a year ...

Discover More

Importing a Subset of Records

If you only want to import a portion of whatever records are in a text file, Excel provides a number of ways you can ...

Discover More

Searching for All

When you are working on a worksheet (particularly a large one), you may want to search for and possibly copy information ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 7?

2018-09-27 17:47:17

Kevin

I agree with Jeff and Rod - something's not right with that SSN2Text script.


2015-02-26 12:12:39

Nora Abbott

If you don't want to use a macro, append an apostrophe to the beginning of the number.
Assume Labels in Row 1 and numbers formatted as text (73023, 43501, 05454, etc.) in Column K with K1 holding the label ZIP. Insert 2 new columns L and M. In L1 and M1, enter some labels such as APOS and NEWZIP. In L2 enter "'" (that's quotes, apostrophe, quotes). In M2, enter the formula =H1&J1. Copy L2 and M2 down the column. Import to Access and delete the fields ZIP and APOS. The NEWZIP field will come in as text, complete with the leading zeros.


2012-01-26 13:44:36

Jeff Crater

Isn't the third parameter in the MID statement unnecessary in this context? If it truly is necessary then why use 99? Most likely a value of 99 would suffice, but wouldn't Len(c)-1 be better?


2012-01-21 13:41:22

Dominick A. Donoflio

Rod is right. Unless I missed something, SSN2TEXT will not add an apostrophe if one is removed at all. Hence the number is left with no apostrophe, defeating the purpose.


2012-01-21 09:02:27

Rod Grealish

In subroutine SSN2Text a leading apostrophe, if present, is removed. Otherwise (Else) a leading apostrophe is added.

In subroutine ZIP2Text the leading apostrophe, if present, is removed. Whether or not a leading apostrophe was removed one is added. This is different from how apostrophes are handled in subroutine SSN2Text.

I think that SSN2Text should be modified by replacing the Else by the End If thus causing the statement

c = "'" & Right("000000000" & c, 9)

to be uncondionally executed i.e. the apostrophe is added whether or not one was removed.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.