英文:
How can I reference a specific column of a selected object in a VBA listbox?
问题
SalesForm.BHSDTAPNAMELF.Value = Application.XLookup(Val(从所选行获取的值), Worksheets("MASTER").Range("S:S"), Worksheets("MASTER").Range("T:T"))
英文:
I have a listbox compiling information from a worksheet onto a userform. I am trying to figure out how to use the value of a column from the selected object on the listbox.
For instance, this is my listbox:
I want to get the value in the phone column from a selected row to plug into an xlookup code I already have written:
SalesForm.BHSDTAPNAMELF.Value = Application.XLookup(Val(*value from selected row*), Worksheets("MASTER").Range("S:S"), Worksheets("MASTER").Range("T:T"))
答案1
得分: 1
假设列表框的MultiSelect属性设置为fmMultiSelectSingle,您可以使用列表框的Column属性来返回所选行中指定列的值。
因此,由于电话列是第7列,且列索引从0开始,您可以如下检索所需的值...
Dim selectedItem As String
selectedItem = UserForm1.ListBox1.Column(6)
实际上,您可以使用Me关键字来引用用户窗体...
Dim selectedItem As String
selectedItem = Me.ListBox1.Column(6)
相应地更改用户窗体和列表框的名称。
英文:
Assuming that the MultiSelect property of the listbox is set to fmMultiSelectSingle, you can use the Column property of the listbox to return the value from the specified column of the selected row.
Therefore, since the phone column is the 7th column, and since the column indexing starts at 0, you can retrieve the desired value as follows...
Dim selectedItem As String
selectedItem = UserForm1.ListBox1.Column(6)
Actually, you can use the Me keyword to refer to the userform...
Dim selectedItem As String
selectedItem = Me.ListBox1.Column(6)
Change the name of the userform and listbox accordingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论