+ Reply to Thread
Results 1 to 4 of 4

Loop variable subscript out of range

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2015
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    2

    Loop variable subscript out of range

    I can't discover what is wrong in my code.
    ArraySeasonality(M) is constantly saying that the subscript is out of range, yet I let M (it was I before but I tried a different variable, still didn't work) loop from 0 to 3 and ArraySeasonality has 4 datapoints (0,1,2,3)
    It is probably something I do wrong in the way I write the loops. When I remove the I=0 lines then the other variables show the subscript out of range as well.

     'All Declarations
    Dim Startrow As Long 'row begin variable
    Dim Endrow As Long 'row end variable
    Dim ColumnForecasting As Integer
    Dim I As Long
    Dim M As Long
    Dim ArrayDemand(7) As Single
    Dim ArrayNewSeries(7) As Single
    Dim ArraySmoothedDemand(7) As Single
    Dim ArraySeasonality(3) As Variant
    Dim VarNew As Integer
    Dim VarDesDemand As Single
    Dim VarMovingAverage As Single
    Dim VarSmoothedMovingAverage As Single
    Dim VarMovingstdv As Single
    Dim VarSmoothedDemand As Single
    Dim VarMovingAverageSeasonality As Single
    Dim VarRunrate As Single
    
    
    'Timestamp
    Range("A1") = Now()
    Startrow = 16
    Endrow = Range("E7").End(xlDown).Row - N
    
    'Current row to last row
    For N = Startrow To Endrow
        
        'Deseasonalizing demand
        VarDesDemand = Cells(N, 5) / Cells(N, 11)
        I = 0
        For I = LBound(ArrayNewSeries) To UBound(ArrayNewSeries)
            ArrayNewSeries(I) = Cells(N - I, 7)
        Next I
    
        'Check if 8 periods are known since beginning of new series
        If WorksheetFunction.Sum(ArrayNewSeries) = 0 Then
        
            'Retrieving last 8 deaseasonalized demand points
            I = 0
            For I = LBound(ArrayDemand) To UBound(ArrayDemand)
                ArrayDemand(I) = Cells(N - I, 30)
            Next I
    
            'Taking the moving average and moving standard deviation
            VarMovingAverage = WorksheetFunction.Average(ArrayDemand)
            VarMovingstdv = WorksheetFunction.StDevP(ArrayDemand)
                
            'Smoothing the demand with upper and lower bound
            Select Case ArrayDemand(0)
                Case Is >= VarMovingAverage + VarMovingstdv
                VarSmoothedDemand = VarMovingAverage + VarMovingstdv
                Case Is <= VarMovingAverage - VarMovingstdv
                VarSmoothedDemand = VarMovingAverage - VarMovingstdv
                Case Else
                VarSmoothedDemand = ArrayDemand(0)
            End Select
            Cells(N, 33) = VarSmoothedDemand
    
            'Runrate
            I = 0
            For I = LBound(ArraySmoothedDemand) To UBound(ArraySmoothedDemand)
                MsgBox (I)
                ArraySmoothedDemand(I) = Cells(N - I, 33)
            Next I
            
            M = 0
            For M = LBound(ArraySeasonality) To UBound(ArraySeasonality)
                ArraySeasonality(M) = Cells(N + 1 + M, 11)
            Next M
            VarSmoothedMovingAverage = WorksheetFunction.Average(ArraySmoothedDemand)
            VarMovingAverageSeasonality = WorksheetFunction.Average(ArraySeasonality)
            VarRunrate = VarSmoothedMovingAverage * VarMovingAverageSeasonality
    
        Else
        
            VarMovingAverage = 0
            VarMovingstdv = 0
            VarSmoothedDemand = 0
            VarRunrate = 0
        End If
         
    Cells(N, 30) = VarDesDemand
    Cells(N, 31) = VarMovingAverage
    Cells(N, 32) = VarMovingstdv
    Cells(N, 34) = VarRunrate
    Next N

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop variable subscript out of range

    Hi, and welcome to the forum.

    It would have been more useful if you had uploaded the workbook so that we can see the problem in context.

    However I suspect it's probably because you have only dimmed the array as 3 despite you saying there are 4 values - and I can see no Redim instruction.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    06-01-2015
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    2

    Re: Loop variable subscript out of range

    Thank you for your reaction.
    However that can't be the problem can it? An array of 3 by default has 4 values? (0,1,2,3)
    Anyway I fixed the problem by just changing all the if statements to do while statements, still a bit weird.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop variable subscript out of range

    Hi,

    Depends on whether the Base is zero or one.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Help reading For loop with range variable
    By Maradona_10 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2014, 02:49 PM
  2. Do Loop with variable range
    By Austex_egger in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-08-2013, 03:08 PM
  3. Do Loop with Variable Range
    By Austex_egger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2013, 03:52 PM
  4. [SOLVED] Select Sheets based on variable array -ERROR 9 Subscript out of range
    By airmiles in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2012, 07:28 PM
  5. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM

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