VBA宏。使用if语句时赋值给Array并显示在消息框中

VBA Macro. Assign to Array when if statement is used and display in message box

将 NumberOfErrors 调暗为整数

    NumberOfErrors = 0

    If Dir(FilePath) = "" Then ' This is declared earlier and working ok

    NumberOfErrors = NumberOfErrors +1

    Dim ErrorArray() As Integer
    ReDim ErrorArray(NumberOfErrors)

    ErrorArray = Cells(cell.Row, "C").Value

    End If
Next cell

If NumberOfErrors > 0 Then

       MsgBox "Attachments Do Not Exist for The Following Users: " & ErrorArray(NumberOfErrors)

End If

所以只是为了尝试澄清我正在尝试做的事情。如果该文件不存在,则创建一个动态数组以显示不存在文件的名称。将它们传递给数组并在消息框中显示不存在文件的列表

你的数组不是动态的,因为你总是将它的大小设置为 4,但你应该将 Dim 行移出循环,然后使用:

ReDim Preserve ErrorArray(NumberOfErrors)

ErrorArray(NumberOfErrors) = Cells(cell.Row, "C").Value

或者您也可以使用集合或字典而不是数组。

为了将数组与 MsgBox 一起使用,您需要使用 Join:

MsgBox Join(ErrorArray, vbLf)

例如。