Help fixing 'Subscript out of range' error

cvrband

Board Regular
Joined
Jan 6, 2016
Messages
63
Office Version
  1. 365
Platform
  1. Windows
I'm trying to teach myself about arrays and can't seem to figure this one out. (many Google searches)

I have the following code that I'm getting a 'subscript out of range' error:

<subscript out="" of="" range="">
Code:
Dim LastRow As Integer
Dim MyArray As Variant
Dim i As Integer

With Sheets("Testing")[INDENT]LastRow = .cells(.Rows.Count, 1).End(xlUp).Row

MyArray = Range("A5:A" & LastRow).Value

[/INDENT]
End With

For i = 0 To UBound(MyArray) - 1[INDENT]If IsNumeric(MyArray(i)) Then
(More code here)
[/INDENT]
Next
End Sub

In the "watch" window, MyArray is populating correctly and UBound(MyArray) provides the correct value, but the code produces an error at the 'If IsNumeric' line. "MyArray(i) = Subscript out of range <subscript out="" of="" range=""><subscript out="" of="" range="">" shows up when I hover over 'MyArray(i)'. What am I doing wrong?

</subscript>Thank you in advance for helping.</subscript></subscript>
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Whenever you populate an array direct from the sheet as you have, the default for the array is Base 1, so you need to change
Code:
For i = 0 To UBound(MyArray) - 1
to
Code:
For i = 1 To UBound(MyArray)

Also the array will be a 2d array so you need to use
Code:
If IsNumeric(MyArray(i,1)) Then
 
Upvote 0
Additional to Fluff's comments:

Code:
Sub test()
  Dim LastRow As[COLOR=#ff0000] Long ' Integer only reaches [FONT=&quot]32,767[/FONT][/COLOR]
  Dim MyArray As Variant
  Dim i As [COLOR=#ff0000]Long[/COLOR]
  
  With Sheets("Testing")
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    MyArray = [SIZE=2][COLOR=#ff0000][B].[/B][/COLOR][/SIZE]Range("A5:A" & LastRow).Value  '[COLOR=#ff0000]Missing point[/COLOR]
  End With
  
  For i = [COLOR=#ff0000]1 To UBound(MyArray)[/COLOR]
    If IsNumeric([COLOR=#ff0000]MyArray(i, 1)[/COLOR]) Then
      MsgBox [COLOR=#ff0000]MyArray(i, 1)[/COLOR]
    End If
  Next
End Sub
 
Last edited:
Upvote 0
Thank you both for your (extremely fast) responses. No more error :) . With all of my searches, I don't think that I ever came across the notation that pulling directly from the sheet was by default Base 1 - now I know. Thanks again!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,303
Members
449,218
Latest member
Excel Master

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