+ Reply to Thread
Results 1 to 5 of 5

Redim Preserve array: subscript out of range

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2011
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    56

    Redim Preserve array: subscript out of range

    Hi all,

    For checking the answer to a multiple choice quiz, I wrote a subroutine. See below.
    (Undefined variables and FaultArray() are defined as Public at the top of the module.)
    What's supposed to happen is that whenever a wrong answer is given, FaultArray (a 2D array, 3 elements wide) gets a new line.
    On this line will be written
    1) the question that was asked
    2) the answer that the user gave
    3) the correct anser
    So that I can present the answers to the wrong questions after the quiz.

    Problem is: the second time the sub processes a wrong answer, I get a 'subscript out of range' error.
    Am I overlooking something? I'm stomped

    Sub CheckAnswerA()
    
    If Worksheets("quiz").Range("K3").Value = Answer Then
        Score = Score + 1
        Range("K7").Value = Score
        Call Again
        Else:
        Fault = Fault + 1
        ReDim Preserve FaultArray(1 To Fault, 1 To 3)
            FaultArray(Fault, 1) = Question
            FaultArray(Fault, 2) = Worksheets("Quiz").Range("K3").Value
            FaultArray(Fault, 3) = Answer
        Call Again
    End If
    
    End Sub
    quiz v10b.xlsm

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Redim Preserve array: subscript out of range

    You can only change the last dimension of a dynamic array when you use Preserve. See Help for ReDim.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Redim Preserve array: subscript out of range

    If you include the Preserve keyword, Visual Basic copies the elements from the existing array to the new array. When you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same size it already has in the existing array.
    If solved remember to mark Thread as solved

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Redim Preserve array: subscript out of range

    something like this

    Sub CheckAnswerA()
    Dim FaultArray As Variant
    
    ReDim FaultArray(1 To 3, 1 To 1)
    If Worksheets("quiz").Range("K3").Value = Answer Then
        Score = Score + 1
        Range("K7").Value = Score
        Call Again
        Else:
            FaultArray(1, UBound(FaultArray, 2)) = Question
            FaultArray(2, UBound(FaultArray, 2)) = Worksheets("Quiz").Range("K3").Value
            FaultArray(3, UBound(FaultArray, 2)) = Answer
            ReDim Preserve FaultArray(1 To 3, 1 To UBound(FaultArray, 2) + 1)
        Call Again
    End If
    
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    02-05-2011
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Redim Preserve array: subscript out of range

    Ah! Excellent, that clears it up. Thanks Mike for your suggestion.

+ 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