vba 转到列表框中的选定项目

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

Go To selected item in listbox

excel-vbavbaexcel

提问by sglxl

I have a ListBox which is populated with various data plus the Row number where the data is located, like so:

我有一个 ListBox,其中填充了各种数据以及数据所在的行号,如下所示:

31-Mar-12 to 15-Apr-12 [Goods A], Row:12009

31-Mar-12 to 15-Apr-12 [Goods A], Row:12009

On a Command Button I need to write code which when clicked will take the user to the specific Row. Column is always fixed at Column 2

在命令按钮上,我需要编写代码,点击后会将用户带到特定的行。列始终固定在第 2 列

采纳答案by Ross McConeghy

If something is selected in the listbox it will find the value after the colon (:) and select column 2 of that row number:

如果在列表框中选择了某些内容,它将在冒号 (:) 之后找到值并选择该行号的第 2 列:

Private Sub CommandButton1_Click()
    Dim selectedItem As String
    If ListBox1.ListIndex <> -1 Then
        selectedItem = ListBox1.List(ListBox1.ListIndex)
        Cells(CInt(Mid(selectedItem, InStrRev(selectedItem, ":") + 1)), 2).Activate
    End If
End Sub