vba 在单元格中显示单击的列表框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22836577/
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 02:39:05  来源:igfitidea点击:

Display clicked List Box value in cell

excelvbalistbox

提问by Josh

I have a Form Controllist box with multiple items.

我有一个包含多个项目的表单控件列表框。

I would like a certain cell on the worksheet to display the contents of the list box item the user selects and change if the user makes a different selection.

我希望工作表上的某个单元格显示用户选择的列表框项目的内容,并在用户进行不同选择时进行更改。

回答by Alex

I guess you already know how to write to a cell:

我想您已经知道如何写入单元格:

Set TxtRng = ActiveWorkbook.Sheets("YourSheet").Range("A1")
TxtRng.Value = "Your Text Here"

And the following piece of code would go into your ListBoxChanged-Event, so the cell displays the new value every time a new value has been selected in the List Box.

以下代码将进入您的ListBoxChanged-Event,因此每次在列表框中选择新值时,单元格都会显示新值。

For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
        SelectedItemText = ListBox1.List(i)
    End If
Next i
'Set the value of the cell to the selected item.
TxtRng.Value = SelectedItemText 

Hope this helps.

希望这可以帮助。