L
Little Penny
This macro exports my spread sheet to a fixed with prn file. It works
great if my array is
myColWidths = Array(69, 13, 11, 48, 18, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 26, 127, 64, 71, 1)
But if I change it to:
myColWidths = Array(69, 13, 11, 19, 29, 18, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 26, 127, 135, 1)
To accommodate different column widths I get a Run-time error '9':
Subscript out of range and the code stops here:
cellStr = Left(Rng(iRow, iCol).Text & _
Space(myColWidths(iCol)), myColWidths(iCol))
I can’t figure out why. Any help would be great appreciated. My entire
code is as follows:
Sub SaveAsFixedWidthSummitLeh()
Dim Rng As Range
Dim myColWidths As Variant
Dim iCol As Long
Dim iRow As Long
Dim myFileName As Variant
Dim myFileNum
Dim resp As Long
Dim myStr As String
Dim cellStr As String
myColWidths = Array(69, 13, 11, 19, 29, 18, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 26, 127, 135, 1)
With ActiveSheet
Set Rng = .UsedRange 'try to reset last used cell.
Set Rng = Range("a1", .Cells.SpecialCells(xlCellTypeLastCell))
End With
Do
myFileName = Application.GetSaveAsFilename( _
fileFilter:="Prn Files, *.Prn", _
InitialFileName:="SavedFile.Prn", Title:="Save
A Range")
If myFileName = False Then End
If Dir(CStr(myFileName)) <> "" Then
resp = MsgBox(Prompt:="Overwrite the existing file?", _
Buttons:=vbCritical + vbYesNoCancel)
Select Case resp
Case Is = vbCancel
MsgBox "Try Later"
Exit Sub
Case Is = vbYes: Exit Do
End Select
Else
Exit Do
End If
Loop
myFileNum = FreeFile()
Close #myFileNum
Open myFileName For Output As #myFileNum
For iRow = 1 To Rng.Rows.Count
If (iRow \ 50) * 50 = iRow Then
Application.StatusBar = "Processing row: " _
& iRow & " at: " & Now
End If
myStr = ""
For iCol = 1 To Rng.Columns.Count
If Application.IsNumber(Rng(iRow, iCol).Value) Then
'right justify the contents
cellStr = Right(Space(myColWidths(iCol)) & _
Rng(iRow, iCol).Text, myColWidths(iCol))
Else
'text is justified to the left??
cellStr = Left(Rng(iRow, iCol).Text & _
Space(myColWidths(iCol)),
myColWidths(iCol))
End If
myStr = myStr & cellStr
Next iCol
Print #myFileNum, myStr
Next iRow
Close #myFileNum
MsgBox "Done at: " & Now
With Application
.StatusBar = False
End With
End Sub
Thanks
great if my array is
myColWidths = Array(69, 13, 11, 48, 18, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 26, 127, 64, 71, 1)
But if I change it to:
myColWidths = Array(69, 13, 11, 19, 29, 18, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 26, 127, 135, 1)
To accommodate different column widths I get a Run-time error '9':
Subscript out of range and the code stops here:
cellStr = Left(Rng(iRow, iCol).Text & _
Space(myColWidths(iCol)), myColWidths(iCol))
I can’t figure out why. Any help would be great appreciated. My entire
code is as follows:
Sub SaveAsFixedWidthSummitLeh()
Dim Rng As Range
Dim myColWidths As Variant
Dim iCol As Long
Dim iRow As Long
Dim myFileName As Variant
Dim myFileNum
Dim resp As Long
Dim myStr As String
Dim cellStr As String
myColWidths = Array(69, 13, 11, 19, 29, 18, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 26, 127, 135, 1)
With ActiveSheet
Set Rng = .UsedRange 'try to reset last used cell.
Set Rng = Range("a1", .Cells.SpecialCells(xlCellTypeLastCell))
End With
Do
myFileName = Application.GetSaveAsFilename( _
fileFilter:="Prn Files, *.Prn", _
InitialFileName:="SavedFile.Prn", Title:="Save
A Range")
If myFileName = False Then End
If Dir(CStr(myFileName)) <> "" Then
resp = MsgBox(Prompt:="Overwrite the existing file?", _
Buttons:=vbCritical + vbYesNoCancel)
Select Case resp
Case Is = vbCancel
MsgBox "Try Later"
Exit Sub
Case Is = vbYes: Exit Do
End Select
Else
Exit Do
End If
Loop
myFileNum = FreeFile()
Close #myFileNum
Open myFileName For Output As #myFileNum
For iRow = 1 To Rng.Rows.Count
If (iRow \ 50) * 50 = iRow Then
Application.StatusBar = "Processing row: " _
& iRow & " at: " & Now
End If
myStr = ""
For iCol = 1 To Rng.Columns.Count
If Application.IsNumber(Rng(iRow, iCol).Value) Then
'right justify the contents
cellStr = Right(Space(myColWidths(iCol)) & _
Rng(iRow, iCol).Text, myColWidths(iCol))
Else
'text is justified to the left??
cellStr = Left(Rng(iRow, iCol).Text & _
Space(myColWidths(iCol)),
myColWidths(iCol))
End If
myStr = myStr & cellStr
Next iCol
Print #myFileNum, myStr
Next iRow
Close #myFileNum
MsgBox "Done at: " & Now
With Application
.StatusBar = False
End With
End Sub
Thanks