无法从 Excel 用户窗体中的多选列表框中获取所选项目的列表
Can't get listed of selected items from multiselect ListBox in Excel userform
VBA 的新手,在从 ListBox 中获取所选项目时遇到一些困难。我一直在 For 行上收到错误。 ListBox 的名称是正确的,我认为 .ListCount 应该可以。
Sub GetListBox1()
Dim SelectedItems As String
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected = True Then
SelectedItems = SelectedItems & ListBox1.List(i)
End If
Next i
Debug.Print SelectedItems
End Sub
试试下面的代码,将 "UserForm1"
切换为您的表单名称。
Dim SelectedItems As String
With UserForm1 ' replace with the name of your form
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then ' <-- you need to add the index of the selected item (according to the loop)
SelectedItems = SelectedItems & .ListBox1.List(i)
End If
Next i
End With
感谢@Sai Rado 的工作。试图用逗号分隔它们,但无法正常工作。
Sub GetListBox1()
Dim SelectedItems As String
Dim SelectedCounter As Integer
SelectedCounter = 0
With UserForm1
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then
SelectedCounter = SelectedCounter + 1
If SelectedCounter = .ListBox1.Selected.ListCount Then
SelectedItems = SelectedItems & .ListBox1.List(i)
Else
SelectedItems = SelectedItems & .ListBox1.List(i) & ", "
End If
Next i
End With
End Sub
要在输出中添加逗号,试试这个...
Dim SelectedItems As String
With UserForm1 ' replace with the name of your form
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then ' <-- you need to add the index of the selected item (according to the loop)
SelectedItems = SelectedItems & .ListBox1.List(i) & ", " ' <-- THE COMMA goes here
End If
Next i
End With
您在 ELSE 子句中的做法只会在列表框项目未被选中时添加一个逗号,并且将为每个未被选中的列表框项目添加一个逗号。
因此,如果用户从州列表框选项中选择了阿拉巴马州和蒙大拿州,则输出将显示这两个州的名称以及 48 个逗号。
VBA 的新手,在从 ListBox 中获取所选项目时遇到一些困难。我一直在 For 行上收到错误。 ListBox 的名称是正确的,我认为 .ListCount 应该可以。
Sub GetListBox1()
Dim SelectedItems As String
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected = True Then
SelectedItems = SelectedItems & ListBox1.List(i)
End If
Next i
Debug.Print SelectedItems
End Sub
试试下面的代码,将 "UserForm1"
切换为您的表单名称。
Dim SelectedItems As String
With UserForm1 ' replace with the name of your form
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then ' <-- you need to add the index of the selected item (according to the loop)
SelectedItems = SelectedItems & .ListBox1.List(i)
End If
Next i
End With
感谢@Sai Rado 的工作。试图用逗号分隔它们,但无法正常工作。
Sub GetListBox1()
Dim SelectedItems As String
Dim SelectedCounter As Integer
SelectedCounter = 0
With UserForm1
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then
SelectedCounter = SelectedCounter + 1
If SelectedCounter = .ListBox1.Selected.ListCount Then
SelectedItems = SelectedItems & .ListBox1.List(i)
Else
SelectedItems = SelectedItems & .ListBox1.List(i) & ", "
End If
Next i
End With
End Sub
要在输出中添加逗号,试试这个...
Dim SelectedItems As String
With UserForm1 ' replace with the name of your form
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then ' <-- you need to add the index of the selected item (according to the loop)
SelectedItems = SelectedItems & .ListBox1.List(i) & ", " ' <-- THE COMMA goes here
End If
Next i
End With
您在 ELSE 子句中的做法只会在列表框项目未被选中时添加一个逗号,并且将为每个未被选中的列表框项目添加一个逗号。
因此,如果用户从州列表框选项中选择了阿拉巴马州和蒙大拿州,则输出将显示这两个州的名称以及 48 个逗号。