vba 如何在 Excel 电子表格的单选列表框中显示所选项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25198718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:08:17  来源:igfitidea点击:

How to display selected item in a single-selection listbox in an excel spreadsheet?

excelvbalistboxselecteditem

提问by Didi_Allo

I am coding a master file on excel, with a header on every sheet that displays data specified through a user form. 3 out of 5 controls work but the listboxes with the start and end dates do not return the selected values. Here are the code lines for the OK button of the user form :

我正在 excel 上编写一个主文件,每个工作表上都有一个标题,显示通过用户表单指定的数据。5 个控件中有 3 个可以工作,但带有开始和结束日期的列表框不返回选定的值。以下是用户表单的 OK 按钮的代码行:

Private Sub OkButton_Click()

Dim s As Integer
Dim lStart As Integer
Dim lEnd As Integer

For s = 2 To 9

    Worksheets(s).Activate
    Cells(1, 2) = CompanyTextBox.Value

    For lStart = 0 To StartListBox.ListCount - 1
    If StartListBox.Selected(lStart) = True Then
    Cells(2, 2) = StartListBox.List(lStart)
    End If
    Next lStart

    For lEnd = 0 To EndListBox.ListCount - 1
    If EndListBox.Selected(lEnd) = True Then
    Cells(3, 2) = EndListBox.List(lEnd)
    End If
    Next lEnd

    Cells(4, 2) = RatingListBox.Value

    Cells(5, 2) = GradeListBox.Value

Next s

Unload Me

End Sub

My question is pretty similar to an unanswered question on stack overflow actually : https://stackoverflow.com/questions/6143420/data-transfer-to-excel-from-visual-basic-2008.

我的问题实际上与堆栈溢出的未回答问题非常相似:https: //stackoverflow.com/questions/6143420/data-transfer-to-excel-from-visual-basic-2008

Also I have tried the Dateand Stringtypes for lStartand lEnd.

我也试过DateandString类型为lStartand lEnd

Thanks in advance for your help !

在此先感谢您的帮助 !

回答by InHoaxidable

As far as I know, the "Selected" property is useful when the ListBox property "MultiSelect" is set to "True". This means that you can select more than one item in a ListBox control.

据我所知,当 ListBox 属性“MultiSelect”设置为“True”时,“Selected”属性很有用。这意味着您可以在 ListBox 控件中选择多个项目。

When "MultiSelect" is set to "False", you'd better use the ListBox property "ListIndex" which returns an index value, between -1 and "ListCount-1", indicating which item in the list has been clicked and is apparent in the control on the userform.

当“MultiSelect”设置为“False”时,最好使用ListBox属性“ListIndex”,它返回一个索引值,介于-1和“ListCount-1”之间,表示列表中的哪一项被点击并且是明显的在用户窗体上的控件中。

When no item is selected, "ListIndex" is equal to -1, the first item selected : 0, the last item selected : "ListCount-1". Hence here is your code modified after what I wrote :

当未选择任何项目时,“ListIndex”等于 -1,选择的第一个项目:0,选择的最后一个项目:“ListCount-1”。因此,这是您在我写的内容后修改的代码:

Private Sub OkButton_Click()

Dim s As Long
Dim lStart As Long
Dim lEnd As Long
Dim WS As Worksheet

For s = 2 To 9

    Set WS = Worksheets(s)

    WS.Cells(1, 2) = WS.CompanyTextBox.Value

    If WS.StartListBox.ListCount > -1 Then
        WS.Cells(2, 2) = WS.StartListBox.List(WS.StartListBox.ListIndex)
    else
        WS.Cells(2, 2) = ""
    End If

    If WS.EndListBox.ListIndex > -1 Then
        WS.Cells(3, 2) = WS.EndListBox.List(WS.EndListBox.ListIndex)
    Else
        WS.Cells(3, 2) = ""
    End If

    WS.Cells(4, 2) = WS.RatingListBox.Value
    WS.Cells(5, 2) = WS.GradeListBox.Value

Next s

Unload Me
Set WS = Nothing

End Sub

Hope it helps...

希望能帮助到你...