I receive files on occasion in a fixed width format. I need to import them into Excel but Excel doesn't perfectly pick up the columns. I can do it manually each time with the Text Import Wizard, but I'm wondering if there is a way to create a "text import template" or something similar – since these files are always the same format.
Excel – Automate Excel Text Import Wizard
automationcsvfixed widthmicrosoft excel
Related Solutions
I think the closest you can get from native Excel functionality is Save As | Formatted Text (Space Delimited) (*.prn). It will automatically determine the widths and insert spaces to pad to that width as necessary.
Beyond that you need to have a macro or other add-in that will let you do more.
This works:
Sub OpenCsvAsText(ByVal strFilepath As String)
Dim intFileNo As Integer
Dim iCol As Long
Dim nCol As Long
Dim strLine As String
Dim varColumnFormat As Variant
Dim varTemp As Variant
'// Read first line of file to figure out how many columns there are
intFileNo = FreeFile()
Open strFilepath For Input As #intFileNo
Line Input #intFileNo, strLine
Close #intFileNo
varTemp = Split(strLine, ",")
nCol = UBound(varTemp) + 1
'// Prepare description of column format
ReDim varColumnFormat(0 To nCol - 1)
For iCol = 1 To nCol
varColumnFormat(iCol - 1) = Array(iCol, xlTextFormat)
' What's this? See VBA help for OpenText method (FieldInfo argument).
Next iCol
'// Open the file using the specified column formats
Workbooks.OpenText _
Filename:=strFilepath, _
DataType:=xlDelimited, _
ConsecutiveDelimiter:=False, Comma:=True, _
FieldInfo:=varColumnFormat
End Sub
Usage:
OpenCsvAsText "C:\MyDir\MyFile.txt"
Comma-separated file is now open as Excel sheet with all columns formatted as text.
Note that @Wetmelon's wizard solution works just fine, but if you're opening many files then you may, like me, grow weary of, each time, scrolling to column 60 in order to Shift-Click it.
EDIT @GSerg states in the comment below that this "doesn't work" and "eats spaces and leading zeroes". I'll just quote the comment to the question, which is more descriptive:
For reasons unknown, even if you explicitly provide formats for all columns in VBA, Excel will ignore it if the file extension is CSV. As soon as you change the extension, that same code will yield the correct results.
So the code above "works", but gets killed by this ridiculous Excel behaviour. Whichever way you cut it, you're stuck having to change the extension to something other than ".csv", sorry! After that, you're home free.
Best Answer
If you record a macro (Tools - Macro - Record New Macro) while you open the file, you will get the parameters you need to use the OpenText method. Here's an example
The FieldInfo argument is the one you care about. It's an array of 2d arrays. The first element is the column and the second is the data type. This example splits the text file in the following columns: 0, 8, 31, 41, 57, 77, 80, 82, 84, and 86.
With that you, you can write a macro to open the text file. See the GetOpenFileName method of the Application object for how to select which file to open.