|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Subscript out of range?
Hi all,
This is probably a stupid question with a simple answer, but for the life of me, I don't see where it's going wrong. I'm filling an array with values from an excel spreadsheet with the following code: Code:
thisRow = 2
While Len(Range("I" & CStr(thisRow)).Value) <> 0
If Range("P" & CStr(thisRow)).Value = (Date - 1) Then
completedCumulative = completedCumulative + 1
cumulativeArray(completedCumulative, 1) = Range("H" & CStr(thisRow)).Value
cumulativeArray(completedCumulative, 2) = Range("I" & CStr(thisRow)).Value
End If
thisRow = thisRow + 1
Wend
The completedCumulative variable is of type Integer and initialized to 0, the array is of type Variant, and I've verified that both cells contain data. However, the first time it starts through the loop, it throws a "Runtime Error 9", stating that the subscript (I'm assuming it means of the array) is out of range. I'm at a loss, and all suggestions are welcome. Thanks and happy holidays! |
|
#2
|
|||
|
|||
|
I don't completely grasp your code, but one suggestion is to remember VB arrays are 0-based where certain collection objects are 1-based. The first array node in vb is array(0)
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
||||
|
||||
|
yep and that means try changing the code to this:
Code:
cumulativeArray(completedCumulative, 0) = Range("H" & CStr(thisRow)).Value
cumulativeArray(completedCumulative, 1) = Range("I" & CStr(thisRow)).Value
|
|
#4
|
|||
|
|||
|
Quote:
<edit> Ok, nevermind. It worked the first time I tried it. Now, it doesn't. I haven't changed any of the other code. Same problem as before, same place as before. The first time it tries to copy fom the spreadsheet into the array, it chokes. I took out the mnemonic constants, thinking they were the culprit(s), and put the numerical values back in, but it's saying that subscript 0,0 is out of range. I have verified that both indicies are set to 0 when it tries to copy, and that there is data in the cell. Array type is still variant. Any other suggestions? Thanks again! </edit> |
|
#5
|
||||
|
||||
|
just wondering, how exactly do you define cumulativeArray?
having it as Variant is not enough, you must say in advance how many items the array can hold. I'm not sure about VB syntax, but here are the two ways to declare array in vbscript: Code:
Dim cumulativeArray(10, 10) or: Code:
Dim cumulativeArray() ReDim cumulativeArray(10, 10) where in the second way it's also possible to put dynamic value (variable) instead of the hard coded numbers. |
|
#6
|
|||
|
|||
|
Quote:
|
|
#7
|
||||
|
||||
|
in your case, you have to first count how many rows you have then you can
set array size: Code:
Dim cumulativeArray()
completedCumulative=0
thisRow=0
While Len(Range("I" & CStr(thisRow)).Value) <> 0
If Range("P" & CStr(thisRow)).Value = (Date - 1) Then
completedCumulative = completedCumulative + 1
End If
thisRow = thisRow + 1
Wend
ReDim cumulativeArray(completedCumulative-1, 1)
completedCumulative=0
thisRow=0
While Len(Range("I" & CStr(thisRow)).Value) <> 0
If Range("P" & CStr(thisRow)).Value = (Date - 1) Then
completedCumulative = completedCumulative + 1
cumulativeArray(completedCumulative, 0) = Range("H" & CStr(thisRow)).Value
cumulativeArray(completedCumulative, 1) = Range("I" & CStr(thisRow)).Value
End If
thisRow = thisRow + 1
Wend
glad I could be the hammer! ![]() |
|
#8
|
|||
|
|||
|
Quote:
|
|
#9
|
|||
|
|||
|
Instead of an array you might consider using a VB collection object.
For VB6 - http://msdn.microsoft.com/library/e...jCollection.asp |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Subscript out of range? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|