简体   繁体   中英

Subscript out of range error specifying the range of the loop of an array

I have a subscript out of range error.

There has to be a problem with the range of the loop of the array, but I feel the range is declared and the corresponding min and max I'm referencing should be at the same location in their respective arrays.

I have tried Redim (but probably not correctly), and specifying explicitly the dimension of the array (1 to 36), but again probably not correctly.

The subscript out of range error occurs at For j = MinAllocation(i) To MaxAllocation(i) .

'declare variables
Dim Sharpe As Variant
Dim CurrentAllocation As Variant
Dim MaxAllocation As Variant
Dim MinAllocation As Variant
Dim TrialAllocation As Variant
Dim BestAllocation As Variant


inc = Sheet1.Range("B59").Value

'populate variables
CurrentAllocation = Sheet1.Range("S2:S37").Value
MaxAllocation = Sheet1.Range("N2:N37").Value
MinAllocation = Sheet1.Range("P2:P37").Value
TrialAllocation = Sheet1.Range("G2:G37").Value

'Populate trial range with current allocation and populate current Sharpe Ratio
Sheet1.Range("G2:G37").Value = CurrentAllocation
Sharpe = Sheet1.Range("B53").Value

'loop through each cell in the array
Dim i As Long
Dim j As Long


For i = LBound(TrialAllocation) To UBound(TrialAllocation)


   'loop through each possible integer in current cell from corresponding minimum to corresponding maximum
    For j = MinAllocation(i) To MaxAllocation(i) Step inc
    TrialAllocation(i) = j

    'check the total of the trial array and populate worksheet with trial data if the total of the array equals 100 and update timer
    Total = WorksheetFunction.Sum(TrialAllocation)


    If Total = 100 Then
    Sheet1.Range("G2:G37").Value = TrialAllocation

    'Determine how many seconds code took to run
    SecondsElapsed = Round(Timer - StartTime, 2)
    Sheet1.Range("Z42").Value = (SecondsElapsed / 86400)

    'store the trial array if the Sharpe Ratio is greater than the previous highest Sharpe Ratio and further constraints are met (B57 = 0)
    If Sheet1.Range("B53").Value > Sharpe And Sheet1.Range("B57").Value = 0 
    Then
    Sharpe = Sheet1.Range("B53").Value
    BestAllocation = Sheet1.Range("g2:g37").Value

    End If
    End If

    Next Step inc
Next

When you take the Value property of a range of multiple contiguous cells, you get a 2D array of the values. If you examine MinAllocation in the watch window, you will see it is something like "Variant/Variant(1 to 36, 1 to 1)"

To get a specific value from the 2D array, you need to specify subscripts for both dimensions of the array. So, MinAllocation(i) would become MinAllocation(i, 1) and so on for the other arrays.

The LBound and UBound functions can take a parameter to specify which dimension to check but, when this is omitted, the first dimension of the specified array is checked. This is why the checks on the bounds of TrialAllocation didn't fail and seemed to produce sensible values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM