Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Preparing Data for Import into Access

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

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2400) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Preparing Data for Import into Access.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Rod Grealish    23 Jan 2012, 12:09
This is a copy of a comment I left on the Ribbon version of this tip as I believe it is relevant here.

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.
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.