要提取VBA列表框的值,可以使用以下方法:
- 使用List属性:使用List属性可以直接获取整个列表框中的所有值。例如:
Dim i As Integer Dim selectedValues As String For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then selectedValues = selectedValues & ListBox1.List(i) & ", " End If Next i selectedValues = Left(selectedValues, Len(selectedValues) - 2) '去掉最后的逗号和空格 MsgBox selectedValues
上述代码会将选中的项的值以逗号分隔的形式存储在selectedValues字符串变量中,并通过MsgBox函数显示出来。
- 使用Value属性:如果只需要获取选中的某一项的值,可以使用Value属性。例如:
Dim selectedValue As String If ListBox1.ListIndex <> -1 Then selectedValue = https://www.yisu.com/ask/ListBox1.Value>上述代码会判断是否选择了某一项,如果选择了,则将该项的值存储在selectedValue字符串变量中,并通过MsgBox函数显示出来。