如何从用户窗体列表框中的 0+n 列中获取文本?

How do I get the text from 0+n columns in a UserForm List Box?

所以这是一个采购订单数据输入应用程序,用户将 PO 中的每个项目输入到用户窗体列表框中,每列中的项目具有不同的属性。例如:

第 1 列:产品名称 第 2 列:购买数量 第 3 列:价格

一切正常,但我无法从列表框中的 0+n 列中获取值。我只找到了这段代码,但它只为我提供了第 n 列的文本值:

ListBox1.ListIndex = n
MsgBox ListBox1.Text

我可以获得我需要的(n,第 0 列)的值。 但是我还需要(n, column 1), (n, column 2)等的值

求助!!!

从多列列表框中的列获取值

您可以使用列表框的列表 属性 从其他列中获取值。

Private Sub CommandButton1_Click()
Dim n As Long

    ' get random number to pull example data from listbox
    n = Application.Randbetween(0, ListBox1.ListCount-1)

    ' Column 1 - name of product
    MsgBox ListBox1.List(n, 0)

    ' Column 2 = Quantity purchased
    MsgBox ListBox1.List(n, 1)


    ' Column 3 = Price
    MsgBox ListBox1.List(n, 2)

End Sub

请注意,ListBox1.Text 和 ListBox1.Value return 值来自的列分别由列表框的 BoundColumn 和 TextColumn 确定。

您可以将列号和行号指定为 ListBox 控件中的 return 值。下面是第 3 列第 3 行 return 值的简单代码。

Note: ListBox indexing always start from zero (0).

Private Sub CommandButton1_Click()
Dim iCol As Long
Dim iRow As Long

    iCol = 2 'Specify column number
    iRow = 2 'Specify row number

    MsgBox ListBox1.List(iRow, iCol)

End Sub

如果要遍历所有元素,则必须使用 For 循环。有关详细信息,请参阅此