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: Aligning Cells when Importing from CSV.

Aligning Cells when Importing from CSV

Written by Allen Wyatt (last updated March 25, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


Marinos works with CSV files a lot. In his case, the CSV files are created by a custom application and he found that he can even include formulae in them. So if a line of the CSV file contains ",,,Total:,=SUM(D5:D13),,,," the formula is evaluated and all is fine. One thing Marinos wants to do, however, is indicate in the CSV file how individual cells should be justified after they are imported into Excel. He seems to remember that in Lotus 123 he could use a prefix character to indicate the alignment of the cell (' for left, ^ for middle, and " for right); he figures the same capability would be great in Excel.

There is no way to do this in Excel; alignment of imported data is based on system defaults, such that text is left-justified and numbers are right-justified. One option, however, would be to add a prefix character that you could then later "parse" with a macro to apply the desired alignment. For instance, you could use "<" for left, "^" for center, and ">" for right. When Excel imports the CSV files, the fields are treated as text. You can then run this macro to search for the leading alignment character and do the desired action:

Sub SetJustification()
    Dim rCell As Range

    For Each rCell In ActiveSheet.UsedRange
        With rCell
            Select Case Left(.Value, 1)
                Case "<"
                    .Value = Mid(.Value, 2)
                    .HorizontalAlignment = xlHAlignLeft
                Case "^"
                    .Value = Mid(.Value, 2)
                    .HorizontalAlignment = xlHAlignCenter
                Case ">"
                    .Value = Mid(.Value, 2)
                    .HorizontalAlignment = xlHAlignCenter
            End Select
        End With
    Next
    Set rCell = Nothing
End Sub

The macro checks each cell in the worksheet. If the cell begins with an alignment character, then the character is removed and the proper alignment is applied.

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 (3131) 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: Aligning Cells when Importing from CSV.

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

Creating a Center Across Selection Button

The ability to center text across a range of cells has long been a staple of experienced Excel users. Here's how to ...

Discover More

Last Paragraph Line Strung Out Incorrectly

Justifying a paragraph may have undesired effects on the last line of a paragraph. If this occurs with your text, you'll ...

Discover More

Finding Odd Values Greater Than 50

If you have a special need to find cell values that meet two different criteria, where to start can be daunting. This tip ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Error Opening Second Workbook

If you try to open a second workbook and you see an error message, it could be because of the way you are opening the ...

Discover More

Personal.xls File Not Opening

The Personal.xls workbook is used primarily to store macros that you want available through all of your workbooks. ...

Discover More

Checking for the Existence of a File

The data stored in a worksheet can often correspond to information external to that worksheet. For instance, you might ...

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 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.