VBA: subscript out of range (runtime error 9)

Aberdham

Board Regular
Joined
Mar 8, 2018
Messages
163
Office Version
  1. 365
Platform
  1. Windows
Hi guys, I would like to use the following code to produce the result of an array using the power of 3. i.e 0, 1.7320, 3, 5.1961 with the power interval of (1/2)

I have come up with the following code but I have come across with runtime error 9 pointing me to the line a(i) = 3^I

Could someone please advise how I can modify the code to get the desired result without changing the original code?

Thank you in advance!!


1631139743872.png
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What result are you expecting from the function? Is it 15522.1266488259?
 
Upvote 0
What is the value of i when the error happens.
 
Upvote 0
What result are you expecting from the function? Is it 15522.1266488259?
1st: 0 (3^0)
2nd: 1.7320 (3^0.5)
3rd: 3 (3^1)
4th: 5.1961 (3 ^ 1.5)
...
with the power interval of (1/2)

like this
 
Upvote 0
Do you have
VBA Code:
Option base 1
at the top of you module? What's the value of Lbound(a) ?
Try this:
VBA Code:
Redim a (0 to n)
For i =0 to n
 
Upvote 0
Do you have
VBA Code:
Option base 1
at the top of you module? What's the value of Lbound(a) ?
Try this:
VBA Code:
Redim a (0 to n)
For i =0 to n
oh almost there, now I have 1(3^0) , 3(3^1) , 9, 27... etc
how can I modify this with an interval of square 1/2 instead of 1
 
Upvote 0
It came out to be all 0?
If you use Option Explicit, it will identify this problem:

Rich (BB code):
Function PowerThree(n)

    Dim a()
    ReDim a(n)
    For i = 0 To n
        a(i) = 3 ^ i
    Nexti
  
    PowerTwelve = a

End Function
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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