无法从Excel用户窗体中的多选列表框获取选定项目的列表 [英] Can't get listed of selected items from multiselect ListBox in Excel userform

查看:137
本文介绍了无法从Excel用户窗体中的多选列表框获取选定项目的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA的新增功能,并且很难从ListBox中获取选定的项目.我在For线上不断收到错误消息. ListBox的名称正确,我认为.ListCount应该可以工作.

New to VBA and having some difficulty getting selected items from a ListBox. I keep getting an error on the For line. The name of the ListBox is correct and I think .ListCount should work.

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".

Try the code below, switch "UserForm1" with the name of your form.

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

这篇关于无法从Excel用户窗体中的多选列表框获取选定项目的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆