Fixed Number of Columns

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

After manipulating data in Excel I would like to copy and paste the data in a text editor. The data should be in fixed format, for example integer numbers should end in columns with multiples of 5. The text is then used as input to an old fashioned program which requires that the data be aligned

I changed the font in Excel to Courrier New and then selected a column width of 5. When I copy the data to a text editor it copies some tab marks and the columns do not align. How can I make things align in the text editor?
 
Vino,

You can use a macro: copy the code below, then paste into a module, and run
it while the sheet with the data is active.

HTH,
Bernie
MS Excel MVP

Sub ExportTo5WideColumnsPRN()

Dim FName As String
Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer

FName = Application.GetSaveAsFilename( _
Left(ActiveWorkbook.FullName, _
Len(ActiveWorkbook.FullName) - 4) & ".prn")

Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile


With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With

Open FName For Output Access Write As #FNum

For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
WholeLine = WholeLine & Right(Space(5) & _
Cells(RowNdx, ColNdx).Text, 5)
Next ColNdx
Print #FNum, WholeLine

Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum
Rows(1).Delete
End Sub

vinoareni said:
After manipulating data in Excel I would like to copy and paste the data
in a text editor. The data should be in fixed format, for example integer
numbers should end in columns with multiples of 5. The text is then used as
input to an old fashioned program which requires that the data be aligned.
I changed the font in Excel to Courrier New and then selected a column
width of 5. When I copy the data to a text editor it copies some tab marks
and the columns do not align. How can I make things align in the text
editor?
 
Back
Top