Formatting question

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

<p> I have a column A, to which I import data from a text file. It misses the leading zeros
For example the value 37833100 should be 037833100 but comes in as 3.78E+07

The column has data with values like 02376RAF9, 912796XP9, 37833100 (mix of both text and numeric) but length is 9

How to format so as to include the leading zeros?
will this help?

Worksheets("SheetName").Range("A:A").Select
Selection.NumberFormat = "@"

</p?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If you want it to actually keep tjhe leading zeroes, then it needs to be handled while importing (explicitly import the field as text)

Once it is already in Excel, it it too late.
Although, you could use a custom number format of "000000000" to display any missing leading zeroes (if you know the length should always be nine and it is just the display and not the actual value you are concerned with).
 
Upvote 0
If you want it to actually keep tjhe leading zeroes, then it needs to be handled while importing (explicitly import the field as text)

Once it is already in Excel, it it too late.
Although, you could use a custom number format of "000000000" to display any missing leading zeroes (if you know the length should always be nine and it is just the display and not the actual value you are concerned with).
I do use the code tpo import it as text. Or is there another way to format the column A while importing?

VBA Code:
strfilename = Application.GetOpenFilename
   
    With ws.QueryTables.Add(Connection:="TEXT;" & strfilename, Destination:=ws.Range("A2"))
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .Refresh

    End With
 
Upvote 0
I typically do not use that method for importing text files, but it looks like from this article, that you should be able to determine the data type of each field you are importing:

Note this section of code here:
Rich (BB code):
Set shFirstQtr = Workbooks(1).Worksheets(1)
Set qtQtrResults = shFirstQtr.QueryTables.Add( _
    Connection := "TEXT;C:\My Documents\19980331.txt",
    Destination := shFirstQtr.Cells(1,1))
With qtQtrResults
    .TextFileParsingType = xlFixedWidth
    .TextFileFixedColumnWidths := Array(5,4)
    .TextFileColumnDataTypes := _
        Array(xlTextFormat, xlSkipColumn, xlGeneralFormat)
    .Refresh
End With
You want to be sure to use the "xlTextFormat" option with the first column to keep the leading zeroes.
 
Upvote 0
I typically do not use that method for importing text files, but it looks like from this article, that you should be able to determine the data type of each field you are importing:

Note this section of code here:
Rich (BB code):
Set shFirstQtr = Workbooks(1).Worksheets(1)
Set qtQtrResults = shFirstQtr.QueryTables.Add( _
    Connection := "TEXT;C:\My Documents\19980331.txt",
    Destination := shFirstQtr.Cells(1,1))
With qtQtrResults
    .TextFileParsingType = xlFixedWidth
    .TextFileFixedColumnWidths := Array(5,4)
    .TextFileColumnDataTypes := _
        Array(xlTextFormat, xlSkipColumn, xlGeneralFormat)
    .Refresh
End With
You want to be sure to use the "xlTextFormat" option with the first column to keep the leading zeroes.
Can you let me know what this is

.TextFileFixedColumnWidths := Array(5,4)
 
Upvote 0
Can you let me know what this is

.TextFileFixedColumnWidths := Array(5,4)
You don't need that part, since you have determined that it is a delimited file with these arguments:
VBA Code:
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
You would use that other one if it was NOT a delimited file (but rather a fixed width file), so each field is a specific number of characters.
 
Upvote 0
You don't need that part, since you have determined that it is a delimited file with these arguments:
VBA Code:
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
You would use that other one if it was NOT a delimited file (but rather a fixed width file), so each field is a specific number of characters.
can I use

.TextFileColumnDataTypes := _
xlTextFormat, xlSkipColumn, xlGeneralFormat
 
Upvote 0
can I use

.TextFileColumnDataTypes := _
xlTextFormat, xlSkipColumn, xlGeneralFormat
No, that is not how you do it. You add the line in red I showed in post 4 to what you currently have, up above the ".Refresh" line.
The array shows the format for each field you are pulling in.

So, the structure would be list this:
Rich (BB code):
    .TextFileColumnDataTypes := _
        Array(format of field 1, format of field 2, format of field 3, etc.)

The number of fields you are matching show match exactly the number of items you have listed in the Array section.

So in the example given in that link:
Rich (BB code):
    .TextFileColumnDataTypes := _
        Array(xlTextFormat, xlSkipColumn, xlGeneralFormat)
It is basically telling you that there are three fields, the first being Text (xlTextFormat), then to skip the second column (xlSkipColumn), and the third is General (xlGeneralFormat).

You haven't told us how many fields (columns) are being imported. If it is only one, then you would just need:
VBA Code:
strfilename = Application.GetOpenFilename
 
    With ws.QueryTables.Add(Connection:="TEXT;" & strfilename, Destination:=ws.Range("A2"))
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .TextFileColumnDataTypes := _
            Array(xlTextFormat)
        .Refresh

    End With
 
Upvote 0
No, that is not how you do it. You add the line in red I showed in post 4 to what you currently have, up above the ".Refresh" line.
The array shows the format for each field you are pulling in.

So, the structure would be list this:
Rich (BB code):
    .TextFileColumnDataTypes := _
        Array(format of field 1, format of field 2, format of field 3, etc.)

The number of fields you are matching show match exactly the number of items you have listed in the Array section.

So in the example given in that link:
Rich (BB code):
    .TextFileColumnDataTypes := _
        Array(xlTextFormat, xlSkipColumn, xlGeneralFormat)
It is basically telling you that there are three fields, the first being Text (xlTextFormat), then to skip the second column (xlSkipColumn), and the third is General (xlGeneralFormat).

You haven't told us how many fields (columns) are being imported. If it is only one, then you would just need:
VBA Code:
strfilename = Application.GetOpenFilename
 
    With ws.QueryTables.Add(Connection:="TEXT;" & strfilename, Destination:=ws.Range("A2"))
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .TextFileColumnDataTypes := _
            Array(xlTextFormat)
        .Refresh

    End With
I import few files and they range from 30-40 fields(columns)
so how to modify the array, if suppose it is 36 fields?
 
Upvote 0
I import few files and they range from 30-40 fields(columns)
so how to modify the array, if suppose it is 36 fields?
You have an array, with 36 values separated by commas, one for each field type.

It is just like I explained in detail in my last post. Please take the time to read through that explanation slowly and carefully.
The example shown was for a file with 3 columns, hence there are 3 values in the array, one for each field (the first one applies to the first field, the second applies to the second field, and the third applies to the third field).
 
Upvote 0

Forum statistics

Threads
1,215,419
Messages
6,124,798
Members
449,189
Latest member
kristinh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top