+ Reply to Thread
Results 1 to 3 of 3

Runtime error '9'. Subscripts out of range - Dynamic array

Hybrid View

  1. #1
    Registered User
    Join Date
    08-20-2007
    Posts
    60

    Runtime error '9'. Subscripts out of range - Dynamic array

    Hello,

    I have defined a dynamic array called Steps(). I am trying to store the value of a cell in it. But I am getting the error: Runtime error '9'. Subscripts out of range. Not sure what's wrong?


    
    X = Range(ActiveCell.Address, ActiveCell.End(xlDown)).Count
    
    Dim Steps() As Integer
    
    For j = 1 To X Step 1
    
        Steps(j - 1) = ActiveCell.Offset(0, 7 + j).Value
        MsgBox Steps(j - 1)
    
    Next j

    Thanks in Advance.. Anil

  2. #2
    Registered User
    Join Date
    08-20-2007
    Posts
    60
    Thanks for looking into it - I figured it out. Had to redim with preserve..

    
    X = Range(ActiveCell.Address, ActiveCell.End(xlDown)).Count
    Dim Steps() As Integer
    For j = 1 To X Step 1
        ReDim Preserve Steps(0 + j)
        Steps(j - 1) = ActiveCell.Offset(0, 7 + j).Value
        MsgBox Steps(j - 1)
    
    Next j
    Thanks, Anil

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You could also use

    Dim Steps(1 to  X) as String
    For j = 1 To X Step 1
        Steps(j) = ActiveCell.Offset(0, 7 + j).Value
        MsgBox Steps(j)
    Next j
    Note:- the array starts at 1

    Here are 2 others ways to fill an array from a sheet without looping through the cells

    Dim Steps As Variant
    Dim X As Long
    X = Range(ActiveCell.Address, ActiveCell.End(xlDown)).Row
    Steps = Range(ActiveCell.Address, Cells(X, ActiveCell.Column)).Value
    Dim Steps As Variant
    Dim Rng As Range
    
    Set Rng = Range(ActiveCell.Address, ActiveCell.End(xlDown))
    
    Steps = Range(ActiveCell.Address, Rng.Address).Value
    Set Rng = Nothing
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1