View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Trevor Shuttleworth Trevor Shuttleworth is offline
external usenet poster
 
Posts: 1,089
Default write out a flat file with fixed columns

Steve

give this a go. You'll need to adjust the spacing as appropriate.

Sub WriteFixedFile()
Dim LastRow As Long
Dim i As Long
Dim sRecord As String
LastRow = Range("A65536").End(xlUp).Row
Open "c:\TESTFILE" For Output As #1 ' Open file for output.
For i = 1 To LastRow
sRecord = Range("A" & i).Value & Space(15 - Len(Range("A" & i)))
sRecord = sRecord & Range("B" & i).Value & Space(5 - Len(Range("B" &
i)))
sRecord = sRecord & Range("C" & i).Value & Space(10 - Len(Range("C" &
i)))
sRecord = sRecord & Range("D" & i).Value & Space(15 - Len(Range("D" &
i)))
'Write #1, sRecord ' data surrounded by quotes
Print #1, sRecord ' no quotes
Next 'i
Close #1
End Sub

Regards

Trevor


"Steve" wrote in message
...
I would like to write a macro that writes a flat file that
is not tab or comma delimited, but has columns start at
specific locations. For example, everything in column A
of the spreadsheet will always start at column 1 of the
flat file. Everything in column B in the spreadsheet will
be written starting in column 15 of the flat file... C in
20, D in 30 ...

What is the code to place the Excel columns into the flat
file columns?

Thanks
Steve