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?
 
one last question

If it is just the first field that needs formatting while others are irrelevant, then can I just do

strfilename = Application.GetOpenFilename

With ws.QueryTables.Add(Connection:="TEXT;" & strfilename, Destination:=ws.Range("A2"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileColumnDataTypes := _
Array(xlTextFormat)
.Refresh

End With

It would be impossible to format the code for all the 36 fields in the array separated by commas!
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If it is just the first field that needs formatting while others are irrelevant, then can I just do
I do not know. Try it and find out!

It would be impossible to format the code for all the 36 fields in the array separated by commas!
That is a bit of an exaggeration. It is quite easy to do with Copy/Paste. I was able to do this in less than one minute:
VBA Code:
With ws.QueryTables.Add(Connection:="TEXT;" & strfilename, Destination:=ws.Range("A2"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileColumnDataTypes = _
    Array(xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat)
.Refresh

End With

One other thing.
Note the documentation shows a colon in front of the equal sign here:
VBA Code:
.TextFileColumnDataTypes :=
but that gave me an error unless I removed it and wrote it like this instead:
VBA Code:
.TextFileColumnDataTypes =
 
Upvote 0
Solution
I do not know. Try it and find out!


That is a bit of an exaggeration. It is quite easy to do with Copy/Paste. I was able to do this in less than one minute:
VBA Code:
With ws.QueryTables.Add(Connection:="TEXT;" & strfilename, Destination:=ws.Range("A2"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileColumnDataTypes = _
    Array(xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, _
    xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat)
.Refresh

End With

One other thing.
Note the documentation shows a colon in front of the equal sign here:
VBA Code:
.TextFileColumnDataTypes :=
but that gave me an error unless I removed it and wrote it like this instead:
VBA Code:
.TextFileColumnDataTypes =
Thanks a lot!

My code worked with your suggestions
 
Upvote 0

Forum statistics

Threads
1,215,705
Messages
6,126,326
Members
449,308
Latest member
Ronaldj

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