Runtime error '9' without an array in sub

Jedrious

New Member
Joined
Aug 6, 2008
Messages
5
I have a macro that is suppossed to import saved data from one xlsm file to another

when running the macro I get
Run-time error '9':
Subscript out of range

when hitting debug on the dialog
RowBounds = Workbooks(OldSheet).Worksheets("Saved Characters").[a1].Value
is highlighted

For reference Worksheets("Saved Characters").[a1] is a CountA function

Code:
Sub Import()
Dim OldSheet As String
Dim RowCount As Long
Dim ColumnCount As Long
Dim RowBounds As Long
OldSheet = Application.GetOpenFilename("Character Sheet Files (*.xlsm), *.txt")
RowBounds = Workbooks(OldSheet).Worksheets("Saved Characters").[a1].Value
For RowCount = 2 To RowBounds
For ColumnCount = 1 To 329
ThisWorkbook.Worksheets("Saved Characters").Cells(RowCount + ThisWorkbook.Worksheets("Saved Characters").[a1].Value, ColumCount) = Workbooks(OldSheet).Worksheets("Saved Characters").Cells(RowCount, ColumCount)
Next
Next
End Sub

When hitting help to discover the nature of the error message it tells me that I have exceeded the bounds of an array or have not specified the number of elements in the array

However I haven't created an array
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Offensive content removed. Poster has been warned. Further transgressions will be rigorously dealt with.
 
Last edited by a moderator:
Upvote 0
I think the getopenfilename method returns a path:
C:\MyBook.xls

You want a workbook name:
MyBook.xls

Maybe:
Code:
OldSheet = Application.GetOpenFilename("Character Sheet Files (*.xlsm), *.txt")
If OldSheet = "" Then
     Exit Sub
Else
     OldSheet = StrReverse(Left(StrReverse(OldSheet), InStr(1, StrReverse(OldSheet), "\") - 1))
End If

Sometimes the error messages are more like guesses... :)
 
Upvote 0
The reason is that GetOpenFileName returns the path to a file, not a file name.

Workbooks(name) must be an open workbook. The name does not include the file path.

Subscript out of Range is the error that is raised when refering to a non-existant member of a collection. Workbooks(as in your case), WorkSheets, Shapes, Collections and others can all throw that error.
 
Upvote 0
I guess I'm assuming you will have the workbook open...

Maybe another workaround is to actually open the other workbook, hide it, get the value, then close it...

HTH - I'm a little foggy on getting values from closed workbooks right now...you could also create a link to it on your sheet.
 
Upvote 0
I think the getopenfilename method returns a path:
C:\MyBook.xls

You want a workbook name:
MyBook.xls

Maybe:
Code:
OldSheet = Application.GetOpenFilename("Character Sheet Files (*.xlsm), *.txt")
If OldSheet = "" Then
     Exit Sub
Else
     OldSheet = StrReverse(Left(StrReverse(OldSheet), InStr(1, StrReverse(OldSheet), "\") - 1))
End If

Sometimes the error messages are more like guesses... :)

The reason is that GetOpenFileName returns the path to a file, not a file name.

Workbooks(name) must be an open workbook. The name does not include the file path.

Subscript out of Range is the error that is raised when refering to a non-existant member of a collection. Workbooks(as in your case), WorkSheets, Shapes, Collections and others can all throw that error.
ok, using a combination of both of your advices(I also tried them individually) I now have the following code


Code:
Sub Import()
Dim OldSheet As Variant
Dim RowCount As Long
Dim ColumnCount As Long
Dim RowBounds As Long
OldSheet = Application.GetOpenFilename("Character Sheet Files (*.xlsm), *.txt")
Workbooks.Open Filename:=OldSheet, ReadOnly:=True
If OldSheet = "" Then
     Exit Sub
Else
     OldSheet = StrReverse(Left(StrReverse(OldSheet), InStr(1, StrReverse(OldSheet), "\") - 1))
End If
RowBounds = Workbooks(OldSheet).Worksheets("Saved Characters").[a1].Value
For RowCount = 2 To RowBounds
For ColumnCount = 1 To 329
ThisWorkbook.Worksheets("Saved Characters").Cells(RowCount + ThisWorkbook.Worksheets("Saved Characters").[a1].Value, ColumCount) = Workbooks(OldSheet).Worksheets("Saved Characters").Cells(RowCount, ColumCount)
Next
Next
Workbooks(OldSheet).Close
End Sub

I'm now getting a 1004 Application-defined or object-defined error

with
ThisWorkbook.Worksheets("Saved Characters").Cells(RowCount + ThisWorkbook.Worksheets("Saved Characters").[a1].Value, ColumCount) = Workbooks(OldSheet).Worksheets("Saved Characters").Cells(RowCount, ColumCount) being highlighted

Which as far as the help file is concerned is VBA's version of "I don't know"
 
Upvote 0
You have mis-spelled ColumnCount in that line. If you put Option Explicit at the top of your module it will force you to declare all variables you use and will catch spelling errors like this rather than assuming you meant to use a new variable.
HTH
 
Upvote 0
You have mis-spelled ColumnCount in that line. If you put Option Explicit at the top of your module it will force you to declare all variables you use and will catch spelling errors like this rather than assuming you meant to use a new variable.
HTH
Thank you so much, no more error messages(now there's just a problem with the math but that's minor)
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top