新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

vba列表框的值怎么提取出来,vba列表框怎么赋值

发布时间:2023-08-08 07:55:31

vba列表框的值怎样提取出来

要提取VBA列表框的值,可使用以下方法:
1. 使用List属性:使用List属性可以直接获得全部列表框中的所有值。例如:
```vba
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函数显示出来。
2. 使用Value属性:如果只需要获得选中的某一项的值,可使用Value属性。例如:
```vba
Dim selectedValue As String
If ListBox1.ListIndex <> -1 Then
selectedValue = ListBox1.Value
MsgBox selectedValue
End If
```
上述代码会判断是否是选择了某一项,如果选择了,则将该项的值存储在selectedValue字符串变量中,并通过MsgBox函数显示出来。