提问人:Tounn 提问时间:10/18/2023 最后编辑:Tounn 更新时间:10/18/2023 访问量:71
VBA:用 ArrayList 填充列表框
VBA : Fill a Listbox with an ArrayList
问:
有人知道我怎样才能将一个添加到用户表单中。
我已经在模块中尝试了此代码,但是当窗口打开时,它不会显示任何内容。ArrayList
ListBox
UserForm1
UserForm1
Public Sub here()
Dim MyList As Object
Set MyList = CreateObject("System.Collections.ArrayList")
MyList.Add "Item1"
MyList.Add "Item2"
MyList.Add "Item3"
UserForm1.ListBox1.List = MyList
End Sub
我读过一些文章,我知道我需要激活库,但不知道我还能做些什么来让它工作。mscordlib.dll
编辑
这是实际程序的用户表单:
答:
2赞
Notus_Panda
10/18/2023
#1
这适用于我调整后的 ..MyList.Array
就像 FaneDuru 说的,你需要以某种方式触发你的潜艇;在本例中,我使用了用户窗体的点击事件。显示 3 个项目唯一需要做的就是单击表格 et 瞧。 通常,您希望将其链接到窗体上或用户窗体初始化中的按钮(单击事件),如下所示:
Private Sub UserForm_Initialize()
here2
End Sub
Private Sub here2()
Dim MyList As Object
Set MyList = CreateObject("System.Collections.ArrayList")
MyList.Add "Item1"
MyList.Add "Item2"
MyList.Add "Item3"
MyList.Add "Yes!"
UserForm1.ListBox1.List = MyList.ToArray
End Sub
评论
0赞
Tounn
10/18/2023
Ty for your anwser 它很好地总结了评论:)
1赞
VBasic2008
10/18/2023
#2
使用数组列表填充列表框
用户窗体1
模块
Private Sub UserForm_Initialize()
Me.ListBox1.List = GetListboxData
End Sub
标准模块,例如 Module1
Function GetListboxData() As Variant
Dim MyList As Object: Set MyList = CreateObject("System.Collections.ArrayList")
MyList.Add "Item1"
MyList.Add "Item2"
MyList.Add "Item3"
GetListboxData = MyList.ToArray
End Function
Sub ShowForm()
With New UserForm1
.Show
' Continue with your code...
End With
End Sub
评论
0赞
Tounn
10/18/2023
完美的其他答案,感谢你们!
评论
UserForm1.ListBox1.List = MyList.ToArray
As Object
CreateObject(..)
Dim Mylist As ArrayList: Set MyList = New ArrayList
UserForm1.ListBox1.List = MyList
UserForm1.ListBox1.List = MyList.ToArray
UserForm1.ListBox1.List = MyList
Me.ListBox1.List
Initialize
UserForm1
UserForm1.ListBox1.List = MyList
UserForm1.ListBox1.List = MyList.ToArray
Me.ListBox1.List = MyList.ToArray
array
ArrayList