-1

I have a code where in one sub I begin to populate an array. Later in another sub I am trying to populate the same array with additional information. It is not working because I am receiving an runtime error 9. I was told that I cannot do this because redim does not work with first dimension of my array.

My question is there another way I can get this done? Below is what I have so far.

The error takes place:

Sub PartTwo()

    ReDim WorkArray(1 To GEMclass.NumofP, 1 To 4)

    For c = 1 To UBound(FullArray, 1)       
        If GEMclass.g = FullArray(c, 1) Then                                        
            Count = Count + 1 

            WorkArray(Count, 1) = FullArray(c, 1)
            WorkArray(Count, 2) = FullArray(c, 4)
            WorkArray(Count, 3) = FullArray(c, 7)
            WorkArray(Count, 4) = FullArray(c, 6)        
        End If        
    Next c

    ReDim Preserve WorkArray(1 To GEMclass.NumofP, 1 To 4)                
    CumulativePartners = GEMclass.NumofP

End Sub

Sub PartThree()

    For e = 1 To Count        
        If dict.Exists(WorkArray(e, 2)) Then

            TempCP = CumulativePartners
            CumulativePartners = CumulativePartners + dict(WorkArray(e, 2)).NumofP                                             
            For f = 1 To UBound(FullArray, 1)

                If WorkArray(e, 2) = FullArray(f, 1) Then                                
                    For g = 1 To dict(WorkArray(e, 2)).NumofP                                    
                        Dim test As Integer
                        test = TempCP + g

                        WorkArray(test, 1) = FullArray(f, 1)<---Runtime error 9
                        WorkArray(test, 2) = FullArray(f, 4)
                        WorkArray(test, 3) = FullArray(f, 7)
                        WorkArray(test, 4) = FullArray(f, 6)                                        
                    Next g                                
                End If

            Next f                
            ReDim Preserve WorkArray(1 To CumulativePartners, 1 To 4)
        Else
            MsgBox "No"
        End If        
    Next e

End Sub
Community
  • 1
  • 1
  • What are the values of `test` and `UBound(WorkArray, 1)` when you get the error? – Comintern Aug 22 '16 at 17:42
  • possible duplicate: http://stackoverflow.com/questions/16369217/redim-preserve-to-a-multi-dimensional-array-in-visual-basic-6 – Jeremy Aug 22 '16 at 17:56
  • Step 1: use `Option Explicit` and understand variable scoping. Where is `WorkArray` declared? Who is calling `PartTwo`? Who is calling `PartThree`? – Mathieu Guindon Aug 22 '16 at 17:57
  • The value of test is 3 and the value. Do you mean the value of ubound Fullarray? That answer is 25 – Freddie Fred Aug 22 '16 at 18:21
  • I am using option explicit. The variables are declared in another sub (Partone). Part one is also calling PartTwo and PartThree. – Freddie Fred Aug 22 '16 at 18:23
  • @FreddieFred - No, I mean `UBound(WorkArray, 1)`. If it's higher than 3, that's why you're getting a subscript out of range. – Comintern Aug 22 '16 at 18:33

1 Answers1

0

Off hand, your scoping is incorrect. Part2 is trying to reference a variable that is only local to Part3. It should look like this:

Option Explicit

Dim WorkArray(1 to GEMclass.NumofP, 1 To 4)

Sub PartTwo()
    'Your code
End Sub

Sub PartThree()
    'Your code
End Sub

If you restructure your declarations like that in the same module, WorkArray will be an available variable to both subroutines.

SandPiper
  • 2,645
  • 4
  • 26
  • 43