Solved

Array in loop

Posted on 2016-09-15
5
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 31

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 31

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 31

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

627 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