Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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: Aligning Cells when Importing from CSV.

Aligning Cells when Importing from CSV

Written by Allen Wyatt (last updated December 12, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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 = xlHAlignRight
            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 (8709) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel 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

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object ...

Discover More

Using a Macro to Change the Formatting of All Instances of a Word

If you have a word that you need to make sure is formatted the same way throughout your document, there are several ways ...

Discover More

Moving Footnote References Using Find and Replace

When you are editing a document, you may need to modify where the author placed footnotes relative to surrounding ...

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)

Getting Input from a Text File

You can use a macro to read information from a text file. The steps are easy, and then you can use that information in ...

Discover More

Who Has the File Open?

Open a workbook that someone else is working on, and you won't be able to save your changes back into the same file. ...

Discover More

Loading Unwanted Files at Startup

Imagine how painful it would be if every time you started Excel it tried to load all the files in your root directory? ...

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 nine minus 5?

2023-12-12 09:27:24

Alex

Another option would be to paste as values-only into a template set up with the desired alignment, if it's a repeated process. Ctl + Shift + V is the kb shortcut for that if anyone, like me a few months ago, didn't know.


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.