我的代码定义如下:

Dim objGroups As Object
Dim arrMemberDN As Variant
Dim arrList() As Variant
Dim i As Integer

Set objGroups = GetObject("LDAP://" & strGroupDN)

For Each arrMemberDN In objGroups.member
    arrMemberDN = Mid(arrMemberDN, 4, 7)
    arrList(i) = arrMemberDN
    i = i + 1
Next arrMemberDN


但是我总是得到


运行时错误9:索引超出范围


arrList(i) = arrMemberDN

如何用arrList中的值填充数组arrMemberDN

最佳答案

首先,您必须为阵列创建尺寸,这里是针对一维阵列。

然后,您Redim Preserve保留其中的内容。

注意:Redim Preserve仅可用于更改LAST尺寸!

Dim objGroups As Object
Dim arrMemberDN As Variant
Dim arrList() As Variant
ReDim arrList(1 To 1)


Set objGroups = GetObject("LDAP://" & strGroupDN)

For Each arrMemberDN In objGroups.member
    arrMemberDN = Mid(arrMemberDN, 4, 7)
    arrList(UBound(arrList)) = arrMemberDN
    ReDim Preserve arrList(LBound(arrList) To UBound(arrList) + 1)
Next arrMemberDN
ReDim Preserve arrList(LBound(arrList) To UBound(arrList) - 1)

10-08 13:31