Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 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.
by Allen Wyatt
(last updated February 1, 2020)
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.
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 Office 365. You can find a version of this tip for the older menu interface of Excel here: Aligning Cells when Importing from CSV.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Import data into a worksheet (or paste it there) and you may find that you end up with a group of blank cells you need to ...Discover More
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
If you have some legacy data stored in very old spreadsheet files, you may be wondering how to get that data into an ...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.