Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
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 he 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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
I think I'd put a debug.print in right before the line that errors out and
figure out which part of it gacks. -- HTH, Barb Reinhardt "Little Penny" wrote: 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 he cellStr = Left(Rng(iRow, iCol).Text & _ Space(myColWidths(iCol)), myColWidths(iCol)) I cant 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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
Hi Little Penny,
Your error appears to be in using the Rng.Columns.Count as the maximum in the For Next Loop and then using the value of iCol from that loop to extract a value from the array myColWidths. The array mycolWidths only has 24 elements and Rng appears to have more columns than the number of elements in myColWidths and hense the value of iCol exceeds the maximum elements of myColWidths. I am not sure if it will fix the problem but maybe you need to look at option base 1 for the array so that it starts from element 1 instead of zero because the error message refers to being out of range in the array. Regards, OssieMac "Barb Reinhardt" wrote: I think I'd put a debug.print in right before the line that errors out and figure out which part of it gacks. -- HTH, Barb Reinhardt "Little Penny" wrote: 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 he cellStr = Left(Rng(iRow, iCol).Text & _ Space(myColWidths(iCol)), myColWidths(iCol)) I cant 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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
first, you need to have Option Base 1 at the top of the module or
myColWidths will start with 0 as the lower bound of the first dimension. I will assume you have that. No reason to believe the usedrange will have equal or fewer columns than myColWidths. So use code like this for your column Loop instead of For iCol = 1 To Rng.Columns.Count try Dim maxcol as Long maxCol = rng.Columns.Count if maxcol ubound(myColWidths) then maxcol = ubound(myColWidths) End if For icol = 1 to maxcol -- Regards, Tom Ogilvy "Little Penny" wrote: 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 he cellStr = Left(Rng(iRow, iCol).Text & _ Space(myColWidths(iCol)), myColWidths(iCol)) I cant 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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
I think you deleted a check from the original post:
http://google.com/groups?threadm=015...0a% 40phx.gbl I'd add this portion back: If Rng.Columns.Count UBound(myColWidths) - LBound(myColWidths) + 1 Then MsgBox "Please contact Brenda at x#### " & vbLf & _ "Tell her you have more columns than she thought were necessary!" Exit Sub End If But change Brenda to your name! And I'd add those two compiler directives back, too: Option Explicit Option Base 1 (Like OssieMac said.) Little Penny wrote: 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 he 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 -- Dave Peterson |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
Adding the
Option Explicit and Option Base 1 back to the code worked. Thanks to everyone On Mon, 10 Sep 2007 21:34:42 -0500, Dave Peterson wrote: I think you deleted a check from the original post: http://google.com/groups?threadm=015...0a% 40phx.gbl I'd add this portion back: If Rng.Columns.Count UBound(myColWidths) - LBound(myColWidths) + 1 Then MsgBox "Please contact Brenda at x#### " & vbLf & _ "Tell her you have more columns than she thought were necessary!" Exit Sub End If But change Brenda to your name! And I'd add those two compiler directives back, too: Option Explicit Option Base 1 (Like OssieMac said.) Little Penny wrote: 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 he 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 |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run-time error '9': Subscript out of range
I'd still add one of those suggested checks to make sure that the array of
columnwidths has enough values for all the columns that may be used. Little Penny wrote: Adding the Option Explicit and Option Base 1 back to the code worked. Thanks to everyone On Mon, 10 Sep 2007 21:34:42 -0500, Dave Peterson wrote: I think you deleted a check from the original post: http://google.com/groups?threadm=015...0a% 40phx.gbl I'd add this portion back: If Rng.Columns.Count UBound(myColWidths) - LBound(myColWidths) + 1 Then MsgBox "Please contact Brenda at x#### " & vbLf & _ "Tell her you have more columns than she thought were necessary!" Exit Sub End If But change Brenda to your name! And I'd add those two compiler directives back, too: Option Explicit Option Base 1 (Like OssieMac said.) Little Penny wrote: 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 he 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 -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
run-time error '9': subscript out of range | Excel Discussion (Misc queries) | |||
run time error #9, subscript out of range | Excel Programming | |||
run time error 9, subscript out of range | Excel Programming | |||
run time error 9, subscript out of range | Excel Programming | |||
Run-time error 9 (Subscript out of range) | Excel Programming |