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.
by Allen Wyatt
(last updated September 27, 2018)
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 (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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
If you are using Excel to work with data that is date-centered, you may want to delete some of the data before a specific ...Discover More
Excel provides keyboard shortcuts for a variety of purposes. This tip examines two such shortcuts, designed to allow ...Discover More
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
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.