Runtime error 9 subscript out of range

vba317

Board Regular
Joined
Oct 7, 2015
Messages
58
I am trying to open up an CSV file in a text delimited format. The file processes alright but when my routine returns to where it came from I get a runtime error 9 on line Set wrkbkOrig = Workbooks(sPracticeSelected).

Any help is appreciated.

Code:
'To open a csv file
If Right(sPracticeSelected, 3) = "csv" Then
 Call OpenCSVFile(sPracticeSelected)
 Set wrkbkOrig = Workbooks(sPracticeSelected)
End If


Public Sub OpenCSVFile(sPracticeSelected As String)
    Dim wrkbkCSV As Excel.Workbook
    Stop
    Set wrkbkCSV = Workbooks.Open(sPracticeSelected)
    Open sPracticeSelected For Input As #1
    row_number = 0
    Do Until EOF(1)
     Line Input #1, LinefromFile
     LineItems = Split(LinefromFile, "|")
     ActiveCell.Offset(row_number, 0).Value = LineItems(0)
     ActiveCell.Offset(row_number, 1).Value = LineItems(1)
     ActiveCell.Offset(row_number, 2).Value = LineItems(2)
     ActiveCell.Offset(row_number, 3).Value = LineItems(3)
     ActiveCell.Offset(row_number, 4).Value = LineItems(4)
     ActiveCell.Offset(row_number, 5).Value = LineItems(5)
     ActiveCell.Offset(row_number, 6).Value = LineItems(6)
     ActiveCell.Offset(row_number, 7).Value = LineItems(7)
     ActiveCell.Offset(row_number, 8).Value = LineItems(8)
     ActiveCell.Offset(row_number, 9).Value = LineItems(9)
     ActiveCell.Offset(row_number, 10).Value = LineItems(10)
     ActiveCell.Offset(row_number, 11).Value = LineItems(11)
     ActiveCell.Offset(row_number, 12).Value = LineItems(12)
     ActiveCell.Offset(row_number, 13).Value = LineItems(13)
     ActiveCell.Offset(row_number, 14).Value = LineItems(14)
     ActiveCell.Offset(row_number, 15).Value = LineItems(15)
     ActiveCell.Offset(row_number, 16).Value = LineItems(16)
     ActiveCell.Offset(row_number, 17).Value = LineItems(17)
     ActiveCell.Offset(row_number, 18).Value = LineItems(18)
     ActiveCell.Offset(row_number, 19).Value = LineItems(19)
     ActiveCell.Offset(row_number, 20).Value = LineItems(20)
     ActiveCell.Offset(row_number, 21).Value = LineItems(21)
     ActiveCell.Offset(row_number, 22).Value = LineItems(22)
     ActiveCell.Offset(row_number, 23).Value = LineItems(23)
'     ActiveCell.Offset(row_number, 24).Value = LineItems(24)
'     ActiveCell.Offset(row_number, 25).Value = LineItems(25)
'     ActiveCell.Offset(row_number, 26).Value = LineItems(26)
'     ActiveCell.Offset(row_number, 27).Value = LineItems(27)
'     ActiveCell.Offset(row_number, 28).Value = LineItems(28)
'     ActiveCell.Offset(row_number, 29).Value = LineItems(29)
'     ActiveCell.Offset(row_number, 30).Value = LineItems(30)


     row_number = row_number + 1
     Loop
     
'     Close #1
     Set wrkbkCSV = Nothing
End Sub


[\code]
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Does sPracticeSelected include the path to the CSV as well as the filename?
 
Upvote 0
You need the path when opening a workbook, you don't need it when trying to refer to it with Workbooks.

Actually I'm a little confused by your code, you appear to be opening the workbook 2 times.

First here,
Code:
Set wrkbkCSV = Workbooks.Open(sPracticeSelected)
then here.
Code:
Open sPracticeSelected For Input As #1
Does that not cause any file access violation type problems?
 
Upvote 0
I figured a way around it. removed the Set wrkbkCSV = Nothing from the sub and I changed the code to
Rich (BB code):
Rich (BB code):
Set wrkbkOrig = wrkbkCSV


Now it works
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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