Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array in loop

Posted on 2016-09-15
5
Medium Priority
?
61 Views
Last Modified: 2016-09-15
hi, experts.

I'm trying to define and use an array in a loop.
so I use
Dim MyArray() as string outside my loop

then in my loop I determine the size and
Redim MyArray (Mycount, 4)

when the loop comes back this redim statement gives a 'subscript out range error'

I have tried adding
Erase MyArray() just before the next

didn't change anything

and I have also tried moving the Dim statement into the loop, same error.

the array is used to read a matrix of data (x rows, 4 columns) to place this on a report in another sheet...
it works fine for the first loop, but apparently the 'redefining' the array is giving the issue.

tx
0
Comment
Question by:Koen
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41799852
Please provide your full code.
0
 
LVL 9

Author Comment

by:Koen
ID: 41799905
    Dim MyArray() As String

    For Count = 1 To ActiveSheet.UsedRange.Rows.Count - 1

    Sheets("Projectfiche").Select
    Range("d6").Value = MyProject
    MyProjectID = Range("D2").Value

    'Define number of Statusses
    Sheets("Status").Select
    j2 = ActiveSheet.UsedRange.Rows.Count
    MyCount = Application.WorksheetFunction.CountIf(Range("Table_owssvr_1[Project Name:ID]"), CStr(MyProjectID))

    ' read statusses into array
    ReDim MyArray(1 To MyCount, 4)
    For i1 = 1 To MyCount
        For i2 = j1 To j2
            If ActiveSheet.ListObjects("Table_owssvr_1").DataBodyRange(i2, 4) = CStr(MyProjectID) Then
                MyArray(i1, 0) = ActiveSheet.ListObjects("Table_owssvr_1").DataBodyRange(i2, 5)
                MyArray(i1, 1) = ActiveSheet.ListObjects("Table_owssvr_1").DataBodyRange(i2, 6)
                MyArray(i1, 2) = ActiveSheet.ListObjects("Table_owssvr_1").DataBodyRange(i2, 8)
                MyArray(i1, 3) = ActiveSheet.ListObjects("Table_owssvr_1").DataBodyRange(i2, 7)
                MyArray(i1, 3) = Right(MyArray(i1, 3), Len(MyArray(i1, 3)) - 2) 'trim return from start
                Exit For
            End If
        Next
        j1 = j1 + 1
    Next

    'place array on projectfiche
    Sheets("Projectfiche").Select
    Range("B49").Select
    For i1 = 1 To MyCount
        ActiveCell.Offset(i1 - 1, 0) = CDate(MyArray(i1, 0))
        ActiveCell.Offset(i1 - 1, 2).Value = MyArray(i1, 1)
        ActiveCell.Offset(i1 - 1, 3).Value = MyArray(i1, 2)
        ActiveCell.Offset(i1 - 1, 5).Value = MyArray(i1, 3)
    Next

    Erase MyArray()
    
Next

Open in new window

0
 
LVL 33

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 total points
ID: 41799928
Make sure that MyCount is not evaluated to 0 before using ReDim MyArray(1 To MyCount, 4).
So wrap your code within an IF statement like this.....

If MyCount > 0 Then
   ReDim MyArray(1 To MyCount, 4)
   For i1 = 1 To MyCount
   '
   '
   '
   '
   '
   '
End If

Open in new window

0
 
LVL 9

Author Closing Comment

by:Koen
ID: 41800006
oh man, now I feel stupid... :-(
0
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41800067
Don't worry. It happens with all of us. :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question