访问 VBA 循环遍历列表框 select 项并添加到数组
Access VBA loop through listbox select items and add to array
我正在尝试遍历列表框并将内容添加到数组中....
我的代码是这样的:
Private Sub exportfolders_Click()
Dim list As String
Dim folderlist As String
Dim folderarray() As String
'Dim i As Interger
For i = 0 To Me.selectedfolders.ListCount - 1
'folderlist = (Me.selectedfolders.Column(0, i))
'folderarray() = Join(Me.selectedfolders.Column(0, i), ",")
list = (Me.selectedfolders.Column(0, i))
folderarray() = Join(list, ",")
ReDim Preserve folderarray(i)
Next i
folderlist = folderarray
'folderarray() = Join(folderlist, ",")
MsgBox (folderlist)
End Sub
你可以看到我已经注释掉的部分,尝试了各种方法让它工作。但我一直在 folderarray(i) = Join(list, ",")
收到消息 "Can't assign to array"。关于我在哪里失败的任何指示?
1) 声明数组。看看https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx
2) 不需要支持变量
3) 使用正确的语法将值分配给数组
Private Sub exportfolders_Click()
Dim folderarray() As String
Dim i As Interger
Redim folderarray (Me.selectedfolders.ListCount-1)
For i = 0 To Me.selectedfolders.ListCount - 1
folderarray(i) = Me.selectedfolders.Column(0, i)
Next i
' Write here what you want to do with your array
End Sub
您可以尝试这样的操作:
Private Sub ListToArray()
Dim folderArray() As Variant
Dim currentValue As String
Dim currentIndex As Integer
Dim topIndex As Integer
topIndex = Me.selectedfolders.ListCount - 1
ReDim folderArray(0 To topIndex, 0 To 1)
For i = 0 To topIndex
currentValue = Me.selectedfolders.Column(0, i)
folderArray(i, 0) = i
folderArray(i, 1) = currentValue
Next i
End Sub
请注意,我的示例是一个多维数组,您可以根据需要添加多个项目。在此示例中,我添加了 "i" 的值作为占位符/索引。
您可以将列表框项连接成一个字符串,然后使用 Split()
加载您的数组。这样,数组会自动调整大小,而无需 ReDim
.
我在 Access 2010 中测试了这段代码:
Dim folderarray() As String
Dim i As Long
Dim strList As String
For i = 0 To Me!selectedfolders.ListCount - 1
strList = strList & "," & Me!selectedfolders.Column(0, i)
Next
' use Mid() to exclude the first comma ...
folderarray = Split(Mid(strList, 2), ",")
请注意,我不知道您要在加载数组后对它做什么。 MsgBox folderarray
会抛出 类型不匹配 错误。 MsgBox Mid(strList, 2)
是有效的,但如果这是您想要的,则不需要数组。
我正在尝试遍历列表框并将内容添加到数组中....
我的代码是这样的:
Private Sub exportfolders_Click()
Dim list As String
Dim folderlist As String
Dim folderarray() As String
'Dim i As Interger
For i = 0 To Me.selectedfolders.ListCount - 1
'folderlist = (Me.selectedfolders.Column(0, i))
'folderarray() = Join(Me.selectedfolders.Column(0, i), ",")
list = (Me.selectedfolders.Column(0, i))
folderarray() = Join(list, ",")
ReDim Preserve folderarray(i)
Next i
folderlist = folderarray
'folderarray() = Join(folderlist, ",")
MsgBox (folderlist)
End Sub
你可以看到我已经注释掉的部分,尝试了各种方法让它工作。但我一直在 folderarray(i) = Join(list, ",")
收到消息 "Can't assign to array"。关于我在哪里失败的任何指示?
1) 声明数组。看看https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx
2) 不需要支持变量
3) 使用正确的语法将值分配给数组
Private Sub exportfolders_Click()
Dim folderarray() As String
Dim i As Interger
Redim folderarray (Me.selectedfolders.ListCount-1)
For i = 0 To Me.selectedfolders.ListCount - 1
folderarray(i) = Me.selectedfolders.Column(0, i)
Next i
' Write here what you want to do with your array
End Sub
您可以尝试这样的操作:
Private Sub ListToArray()
Dim folderArray() As Variant
Dim currentValue As String
Dim currentIndex As Integer
Dim topIndex As Integer
topIndex = Me.selectedfolders.ListCount - 1
ReDim folderArray(0 To topIndex, 0 To 1)
For i = 0 To topIndex
currentValue = Me.selectedfolders.Column(0, i)
folderArray(i, 0) = i
folderArray(i, 1) = currentValue
Next i
End Sub
请注意,我的示例是一个多维数组,您可以根据需要添加多个项目。在此示例中,我添加了 "i" 的值作为占位符/索引。
您可以将列表框项连接成一个字符串,然后使用 Split()
加载您的数组。这样,数组会自动调整大小,而无需 ReDim
.
我在 Access 2010 中测试了这段代码:
Dim folderarray() As String
Dim i As Long
Dim strList As String
For i = 0 To Me!selectedfolders.ListCount - 1
strList = strList & "," & Me!selectedfolders.Column(0, i)
Next
' use Mid() to exclude the first comma ...
folderarray = Split(Mid(strList, 2), ",")
请注意,我不知道您要在加载数组后对它做什么。 MsgBox folderarray
会抛出 类型不匹配 错误。 MsgBox Mid(strList, 2)
是有效的,但如果这是您想要的,则不需要数组。