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
 
Try this:

VBA Code:
Option Explicit
Sub Test()
    
    Dim RES As String
    
    RES = powerThree(8)
    Debug.Print RES

End Sub
Function powerThree(n As Long) As String
    
    Dim i As Currency
    Dim j As Long
    
    For i = 0 To n Step 0.5
        j = j + 1
        powerThree = IIf(Len(powerThree) = 0, Addth(CStr(j)) & ": " & Round(3 ^ i, 4) & " (3^" & i & ")", powerThree & vbNewLine & Addth(CStr(j)) & ": " & Round(3 ^ i, 4) & " (3^" & i & ")")
    Next i

End Function
Function Addth(pNumber As String) As String

    'https://www.extendoffice.com/documents/excel/3757-excel-format-number-1st-2nd-3rd.html
    'UpdatebyExtendoffice20160628
    Select Case CLng(VBA.Right(pNumber, 1))
        Case 1
            Addth = pNumber & "st"
        Case 2
            Addth = pNumber & "nd"
        Case 3
            Addth = pNumber & "rd"
        Case Else
            Addth = pNumber & "th"
    End Select
    
    Select Case VBA.CLng(VBA.Right(pNumber, 2))
        Case 11, 12, 13
            Addth = pNumber & "th"
    End Select
    
End Function
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
VBA Code:
Function PowerThree(n)

    Dim a(0 to n)
 
    For i = 0 To n
        a(i) = 3 ^ (i/2)
    Next i
 
    PowerThree = a

End Function
Or
VBA Code:
Function PowerThree(n)

    Dim a(0 to n)
 
    For i = 0 To n step 0.5
        a(i) = 3 ^ i
    Next i
 
    PowerThree = a

End Function
 
Upvote 0
Solution
Maybe...

VBA Code:
Sub aTest()
    Dim Res As Variant
    
    Res = powerThree(8)
    arrayPrint Res
End Sub

Function powerThree(n As Long)
    Dim a() As Variant, i As Long, j As Double
    
    ReDim a(n)
    For i = 0 To n
        a(i) = 3 ^ j
        j = j + 0.5
    Next i
    powerThree = a
End Function

Sub arrayPrint(Res)
    Dim i As Long
    
    'Header in A1
    Cells(1, 1) = "Power Three"
    'Results in column A beginning in A2
    For i = LBound(Res) To UBound(Res)
        Cells(i + 2, 1) = Res(i)
    Next i
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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