Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Fixed Width - Opening Certain Fixed Width Files

I receive weekly data files from the USPS in a fixed
width format. Each file will have the exact same 23
fields defined when opened (position 1, 9, 16, 32, etc.).
Is there a way to create a macro that will automatically
parse the file at the correct 23 columns so this doesn't
have to be done manually each time I receive a new data
file?
Thanks,
Jan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Fixed Width - Opening Certain Fixed Width Files

(From a previous post--so don't trust the column layout!)
=========


Start a new workbook.
Tools|macro|Record new macro
Do all your importing and reformatting.
Include all the things you like (worksheet headers, print headers/footers/freeze
panes/Data|Filter|Autofilter)

Stop recording and save that workbook with the recorded code in it.
Put a giant button from the forms toolbar on the first worksheet in that
"importer" workbook. Assign your macro to the button.

You'll probably have to adjust the code a little to make it more generic. When
you recorded your macro, you got something that looked like:

Option Explicit
Sub Macro1()

Workbooks.OpenText Filename:="C:\myfile.txt", Origin:=437, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(15, 1), _
Array(41, 1))

End Sub

Well, instead of having your filename in the code, you can give the user a
chance to pick it themselves (take a look at getopenfilename in VBA's help):

Sub macro1A()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub



Jan wrote:

I receive weekly data files from the USPS in a fixed
width format. Each file will have the exact same 23
fields defined when opened (position 1, 9, 16, 32, etc.).
Is there a way to create a macro that will automatically
parse the file at the correct 23 columns so this doesn't
have to be done manually each time I receive a new data
file?
Thanks,
Jan


--

Dave Peterson

  #3   Report Post  
Posted to microsoft.public.excel.programming
Jan Jan is offline
external usenet poster
 
Posts: 159
Default Fixed Width - Opening Certain Fixed Width Files

Dave,

Worked just great. Thanks so much!

Jan

-----Original Message-----
(From a previous post--so don't trust the column layout!)
=========


Start a new workbook.
Tools|macro|Record new macro
Do all your importing and reformatting.
Include all the things you like (worksheet headers,

print headers/footers/freeze
panes/Data|Filter|Autofilter)

Stop recording and save that workbook with the recorded

code in it.
Put a giant button from the forms toolbar on the first

worksheet in that
"importer" workbook. Assign your macro to the button.

You'll probably have to adjust the code a little to make

it more generic. When
you recorded your macro, you got something that looked

like:

Option Explicit
Sub Macro1()

Workbooks.OpenText Filename:="C:\myfile.txt",

Origin:=437, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:=Array(Array

(0, 1), Array(15, 1), _
Array(41, 1))

End Sub

Well, instead of having your filename in the code, you

can give the user a
chance to pick it themselves (take a look at

getopenfilename in VBA's help):

Sub macro1A()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename

(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of

recorded code here!

End Sub



Jan wrote:

I receive weekly data files from the USPS in a fixed
width format. Each file will have the exact same 23
fields defined when opened (position 1, 9, 16, 32,

etc.).
Is there a way to create a macro that will

automatically
parse the file at the correct 23 columns so this

doesn't
have to be done manually each time I receive a new data
file?
Thanks,
Jan


--

Dave Peterson

.

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
Fixed Column Width Export [email protected] Excel Discussion (Misc queries) 1 March 27th 08 01:27 PM
Column fixed width juanpablo Excel Discussion (Misc queries) 1 January 16th 08 12:57 PM
Fixed width fonts Also Excel Discussion (Misc queries) 3 January 8th 08 04:42 PM
Software to download Fixed Width Files create_share Excel Discussion (Misc queries) 1 December 31st 05 09:41 PM
How do I create a "fixed-width" datafile? Alex K Excel Discussion (Misc queries) 4 June 20th 05 01:46 PM


All times are GMT +1. The time now is 05:29 PM.

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

About Us

"It's about Microsoft Excel"