如何在VBA列表框中引用所选对象的特定列?

huangapple go评论91阅读模式
英文:

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: 如何在VBA列表框中引用所选对象的特定列?

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.

huangapple
  • 本文由 发表于 2023年6月9日 06:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定