Solved

Array in loop

Posted on 2016-09-15
5
41 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 28

Expert Comment

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

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 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 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 8

Author Closing Comment

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

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now