vba:Do Loop中的数组不起作用

时间:2014-10-29 22:35:06

标签: excel-vba vba excel

我正在尝试使用项目编号创建一个数组。下面的代码循环遍历国家/地区内的程序列表(在程序内容审核标签中)。对于每个程序,它切换到一个带有项目列表的选项卡(项目摘要统计),我希望它创建一个包含项目编号的数组,以便在单独的代码中我可以调用它。下面的代码正确识别activeprogramnum但是吐出各种奇怪的项目编号。救命啊!

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

1 个答案:

答案 0 :(得分:0)

以下代码有效!!!!我只包括了关键变化。

    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