Run-time error 9 Subscript out of range

arslade

New Member
Joined
Jan 16, 2018
Messages
1
I am getting the subscript out of range message for my VBA code within excel. Can anyone help me figure out where I went wrong in my code?

Private Sub HW2_2()


Dim FileNum As Integer
Dim DataLine As String
Dim col() As String
Dim MyArray(1 To 50, 1 To 15) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim Maximum As Double


FileNum = FreeFile()
Open "C:\Users\slade\Desktop\Spring 2018\ABE 316\HW2\HW02data.txt" For Input As #FileNum


While Not EOF(FileNum)
For i = 1 To 50
For j = 1 To 15
Line Input #FileNum , DataLine
col = Split(DataLine)
MyArray(i, j) = col(1)
Cells(i, j).Value = col(1)
Next j
Next i
Wend


Cells(52, 1).Value = "Batch no."
Cells(52, 3).Value = "Max Value"
k = 52


For i = 1 To 50
Maximum = Cells(i, 1).Value
For j = 1 To 15
If (Maximum < Cells(i, j).Value) Then
Maximum = Cells(i, j).Value
End If
Next j

Cells(k + i, 1).Value = i
Cells(k + i, 2).Value = Maximum
Next i

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Looking at your i,j loop it you have allocated an array of 50 x 15 that's 750 items.
If you have more than 750 records in the file HW02data.txt then you'll get a subscript out of range error as you haven;t defined the array big enough to hold the number of recods HW02data.txt contains.
 
Upvote 0
Where do you get the error?

How are you trying to split the lines you are reading from the file?

Have you tried adding a test to check if the line that's been read has actually been split?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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