Solved

Array in loop

Posted on 2016-09-15
5
51 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 30

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 30

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 30

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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