Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Jerry
 
Posts: n/a
Default automatically open .txt file with all fields as text?

I need to be able to open a comma separated .txt file so that all fields
are automatically formated as text. I need this so that fields such as
"0003" are not converted to numbers which show up as "3". I need to have
it imported as "0003". Since there are over 150 fields per record, using
the import wizard and manually setting each column as text is less than
desirable. Any way to do this automatically?
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

If you record a macro when you open the workbook, you'll see something like:

FieldInfo:=Array(Array(1, 2), ....

That 2 means that field should be treated as text. You can create your own
array so you don't have to do all 256 (potential) fields.


Option Explicit
Sub testme01()

Dim myArray() As Variant
Dim iCtr As Long
Dim maxFields As Long
Dim fName As Variant

fName = Application.GetOpenFilename(filefilter:="Text Files, *.txt")

If fName = False Then
Exit Sub 'user hit cancel
End If

maxFields = 256 '256 columns maximum

ReDim myArray(1 To maxFields, 1 To 2)
For iCtr = 1 To 256
myArray(iCtr, 1) = iCtr
myArray(iCtr, 2) = 2
Next iCtr

Workbooks.OpenText Filename:=fName, Origin:=437, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=myArray

End Sub

Jerry wrote:

I need to be able to open a comma separated .txt file so that all fields
are automatically formated as text. I need this so that fields such as
"0003" are not converted to numbers which show up as "3". I need to have
it imported as "0003". Since there are over 150 fields per record, using
the import wizard and manually setting each column as text is less than
desirable. Any way to do this automatically?


--

Dave Peterson
  #3   Report Post  
Jerry
 
Posts: n/a
Default

Thanks Dave. It works perfectly.

--
Jerry
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Refresh Imported Data - Does the Excel file have to be open? tinkertoy Excel Discussion (Misc queries) 0 June 23rd 05 07:51 PM
Excel opening but file does not open with Application eWisdom Excel Discussion (Misc queries) 1 March 10th 05 05:53 PM
Why Excel file doesn't open by double click. Siraj Excel Discussion (Misc queries) 1 February 6th 05 01:58 PM
How Do I open an excel file without Excel Viewer support CocoriteBallGiants Excel Discussion (Misc queries) 2 February 4th 05 10:50 PM
Cannot open a file that Excel says is open Plodhia Excel Discussion (Misc queries) 2 December 7th 04 01:43 AM


All times are GMT +1. The time now is 10:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"