PDA

View Full Version : Subscript out of range (Error 9)



EML
03-23-2012, 06:34 PM
Subscript out of range (Error 9)
I'm desperate. Can anyone please tell me what is wrong?


Sub LPT()
Dim n As Integer
n = 3

Dim order() As Integer
ReDim order(n, n)
order(0, 0) = 3
order(0, 1) = 1
order(0, 2) = 2
order(1, 0) = 1
order(1, 1) = 2
order(1, 2) = 3
order(2, 0) = 1
order(2, 1) = 2
order(2, 2) = 3

Dim times() As Integer
ReDim times(n, n)
times(0, 0) = 88
times(0, 1) = 10
times(0, 2) = 57
times(1, 0) = 48
times(1, 1) = 9
times(1, 2) = 5
times(2, 0) = 53
times(2, 1) = 36
times(2, 2) = 87

Dim schedule() As Integer
ReDim schedule(n, n)
schedule(0, 0) = 0
schedule(0, 1) = 0
schedule(0, 2) = 0
schedule(1, 0) = 0
schedule(1, 1) = 0
schedule(1, 2) = 0
schedule(2, 0) = 0
schedule(2, 1) = 0
schedule(2, 2) = 0

Dim matrix As Integer
matrix = n * n

For i = 0 To matrix - 1
Dim noPredecessor() As Integer
ReDim noPredecessor(n)

For j = 0 To n - 1
noPredecessor(j) = -1
Next j

For j = 0 To n - 1
For k = 0 To n - 1
If noPredecessor(j) = -1 And order(j, k) < n * n Then
noPredecessor(j) = k
End If
If noPredecessor(j) <> -1 And order(j, k) < order(j, noPredecessor(j)) Then
noPredecessor(j) = k
End If
Next k
Next j

It continues with more lines. But I noticed that If I put "next i" right after "next j", it doesn't work as I want, but I dont get an error.

Kenneth Hobs
03-23-2012, 07:32 PM
You have two loops for j. Obviously, you must have a Next i if you have a For i loop.

Debug should have told you what line your error was. Let me guess, was it:
If noPredecessor(j) = -1 And order(j, k) < n * n Then
In that line, you should note that the 1st dimension of order can only go to 3 while your loop would make it larger. It therefore throws the error because your referenced an element that does not exist.

Also note that, order(0 to 3, 0 to 3) contains 4*4 elements. Use lbound() and ubound() when you don't know the dimensions of an array.

EML
03-24-2012, 02:58 AM
You have two loops for j. Obviously, you must have a Next i if you have a For i loop.
My code is twice as large as the demonstration I posted and "next i" is in the very end of it.


Debug should have told you what line your error was.
Yes, I tried to make it bold, but it's still little numb.
In fact, it was this line:
If noPredecessor(j) <> -1 And order(j, k) < order(j, noPredecessor(j)) Then
Let me guess, was it:
If noPredecessor(j) = -1 And order(j, k) < n * n Then


In that line, you should note that the 1st dimension of order can only go to 3 while your loop would make it larger.
I'm not sure if I follow you. My loop is limited by "n-1" (n=3), that means 3 actions 0, 1, 2.


Also note that, order(0 to 3, 0 to 3) contains 4*4 elements. Use lbound() and ubound() when you don't know the dimensions of an array. Thanks for tip. Is this a core of my problem?

mohanvijay
03-24-2012, 04:36 AM
you set all the element's values in noPredecessor equal to -1



If noPredecessor(j) <> -1 And order(j, k) < order(j, noPredecessor(j)) Then


for the above line noPredecessor(j) returns -1 that is the problem

EML
03-24-2012, 03:55 PM
I don't know. I converted the whole program from java. It works fine there.
I honestly have no idea what is wrong. I fixed somehow that error, worked fine for a while.
I added a user form button that is absolutely not related to the rest of the code and all of the sudden, "subscript out of range" again" - in different place of course! I really need someone to fix it all. I'm desperate.