Run-time error 9: Subscript out of range

croco1

New Member
Joined
Nov 30, 2016
Messages
10
Hi, I tried several solutions but can't find a solution for the error. I'm trying to do a lot of calculations and put them all in a 2-dimensional table. Hopefully someone can help me with the error and perhaps amend the code to speed up the calculations. Thanks for any help!

VBA Code:
Sub UpdateSensitivitiesTables()

    Application.ScreenUpdating = False
    
    Dim Result() As Variant

    EndRow = Range("EndRow").Value
    EndColumn = Range("EndColumn").Value
    StartRow = Range("StartRow").Value
    StartColumn = Range("StartColumn").Value
  
    Worksheets("Sensitivities").Select
    Cells(EndRow - StartRow, EndColumn - StartColumm).ClearContents

    ReDim Result(EndRow - StartRow, EndColumn - StartColumm)
    
    Range("CalculationStartTime") = Time

    For Row = StartRow To EndRow
        Range(Range("ColumnInputVariableRangeName")) = Cells(Row, StartColumn - 1)
        For Column = StartColumn To EndColumn
            Result(Row, Column) = Range(Range("RangeOutputVariableRangeName")) 'Here I get the Run-time error 9: Subscript out of range
            Call RefreshSeniorDebtAmount
            Cells(Row, Column) = Range(Range("RangeOutputVariableRangeName"))
        Next Column
    Next Row
    Cells(EndRow - StartRow, EndColumn - StartColumm) = Result 'Not sure if this will work either after solving the first error.
    Range("CalculationEndTime") = Time
    Calculate
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You have made some typos in your code, so the result is either undefined or gives you run-time errors.
Sometimes you use the variable StartColumn and sometimes StartColumm.
It is highly recommended that you declare all your variables in advance, as well as put the Option Explicit statement at the beginning of each module, so that you are forced to declare variables in advance. Inadvertently not declared variables (due to typing errors) are then quickly revealed.
If you go to Menu> Tools> Options in VBE and then check the option Require Variable Declaration, the Option Explicit statement is automatically added in every new module.

ScreenShot154.png



Regarding your code, I think this
Range (Range ("RangeOutputVariableRangeName"))
can be replaced with this
Range ("RangeOutputVariableRangeName")

Finally, I would note that it is almost never necessary to select ranges using the Select statement.
These lines of code
VBA Code:
Worksheets("Sensitivities").Select
Cells(EndRow - StartRow, EndColumn - StartColumm).ClearContents
could be replaced by this
VBA Code:
Worksheets("Sensitivities").Cells(EndRow - StartRow, EndColumn - StartColumn).ClearContents

Hopefully the previous comments will lead to a solution to your problem.
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,927
Members
448,533
Latest member
thietbibeboiwasaco

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