简体   繁体   中英

vba: Array within Do Loop not working

I am trying to create an array with project numbers. The code below loops through a list of programs within countries (in the program content review tab). For each program it switches to a tab with a list of projects (Project Summary Stats) and I want it to create an array with the project numbers so that in separate code I can call on it. The code below identifies the activeprogramnum correctly but spits out all kinds of strange project numbers. Help!

Sub test_array()
Dim ActiveProgramNum
Sheets("ProgramContentReview").Select
Range("b2").Select ' country

Do Until IsEmpty(ActiveCell)

    Dim ProjectNumArray(10)
    Dim l
    l = 0

    If (Sheets("Introduction").Range("A15").Value & " ") = ActiveCell.Value Then ' This identifies the country

     ActiveProgramNum = ActiveCell.Offset(0, 2)

            Sheets("ProjectSummaryStats").Select
            ' Select cell A2, *first line of data*.
            Range("D2").Select
        Do Until IsEmpty(ActiveCell) ' loop through projects (at program level)

             If ActiveProgramNum = ActiveCell.Value Then

                  ProjectNumArray(l) = ActiveCell.Offset(0, 2).Value ' this is the column with the project numbers
                  l = l + 1
                    MsgBox (ActiveProgramNum)
                    MsgBox (ProjectNumArray(l))

              End If

          ActiveCell.Offset(1, 0).Select
          Loop
End If
Sheets("ProgramContentReview").Select
ActiveCell.Offset(1, 0).Select
Loop

End Sub

The code below works!!!! I only included the key changes.

    Dim ProjectNumArray() As String
    Dim l As Integer
    l = 0

   'If (Sheets("Introduction")...
   'If ActiveProgramNum = ...

                  l = l + 1
                  ReDim ProjectNumArray(1 To l)
                  ProjectNumArray(l) = ActiveCell.Offset(0, 5).Value

   'Rest is the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM