Excel-VBA 根据现有列表更新用户窗体列表框

Excel-VBA update userform listbox based on existing list

我有一个带有多列列表框的 useform,其中列出了项目及其数量。目的是能够通过从文本框输入中添加或减去来动态更新数量。下面是我当前的代码,大致可以实现这一点。到目前为止,它不适用于 selected(i) 的无效限定符错误。将不胜感激关于此的任何指导

    Private Sub CB_AddOrder_Click()

   Dim j, k, qty As Integer
   Dim i As Variant

   qty = TB_Qty.Value

   If qty = 0 Then
   Exit Sub
   End If

   j = LB_Order.ListCount - 1

   Debug.Print j

   If j < 0 Then
   j = 0
   End If

   'Iterate to check if selected menu already existed in ordered list

   For i = 0 To LB_Menu.ListCount - 1
   If LB_Menu.Selected(i) = True Then
   Debug.Print Selected(i)

   For k = 0 To j
   If LB_Menu.Selected(i).List(i, 0) = LB_Order.List(k, 0) Then
   LB_Order.List(k, 3) = LB_Order.List(k, 3).Value + qty
   Exit Sub
   End If
   Next k
        
   With LB_Order
   .ColumnCount = 5
   .ColumnWidths = "120;60;60;60;60"
   .AddItem
   .List(j, 0) = LB_Menu.List(i, 0)
   .List(j, 1) = LB_Menu.List(i, 1)
   .List(j, 2) = LB_Menu.List(i, 2)
   .List(j, 3) = qty
   .List(j, 4) = Format(qty * LB_Menu.List(i, 2), "0.00")
    End With
    End If

    Next i

    End sub

您遇到的困惑与选择列表框项目的差异以及这些所选项目的值有关。所以当你检查 Selected:

Dim i As Long
For i = 0 To LB_Menu.ListCount - 1
    If LB_Menu.Selected(i) Then
        Debug.Print "Menu selected (" & i & ") = " & LB_Menu.List(i, 0)
    End If
Next i

一旦您确定选择了哪个 index(在本例中为 i),您可以通过使用索引进入List.

您收到的 Object Required 错误是因为您的陈述

LB_Order.List(k, 3) = LB_Order.List(k, 3).Value + qty

正在使用 .Value 作为列表项。此项是一个值,而不是一个对象。

这里以你重写的子为例。请注意,我使用单字符变量作为循环索引(这很好),但不是一个有意义的值。我以(希望)有意义的方式重命名了其他变量,以使您的代码更加自我记录。

Option Explicit

Private Sub CB_AddOrder_Click()
    Dim additionalQty As Long
    additionalQty = TB_Qty.Value

    If additionalQty = 0 Then
        Exit Sub
    End If

    Dim countOfOrderItems As Long
    countOfOrderItems = LB_Order.ListCount - 1
    If countOfOrderItems < 0 Then
        countOfOrderItems = 0
    End If

    'Iterate to check if selected menu already existed in ordered list
    Dim i As Long
    For i = 0 To LB_Menu.ListCount - 1
        If LB_Menu.Selected(i) Then
            Debug.Print "Menu selected (" & i & ") = " & LB_Menu.List(i, 0)
            
            '--- find the matching item and increase the quantity
            Dim k As Long
            For k = 0 To countOfOrderItems
                If LB_Menu.List(i) = LB_Order.List(k, 0) Then
                    LB_Order.List(k, 3) = LB_Order.List(k, 3) + additionalQty
                    Exit Sub
                End If
            Next k
        
            '--- append the new item from the Menu to the Order
            With LB_Order
                .ColumnCount = 5
                .ColumnWidths = "120;60;60;60;60"
                .AddItem
                .List(countOfOrderItems, 0) = LB_Menu.List(i, 0)
                .List(countOfOrderItems, 1) = LB_Menu.List(i, 1)
                .List(countOfOrderItems, 2) = LB_Menu.List(i, 2)
                .List(countOfOrderItems, 3) = additionalQty
                .List(countOfOrderItems, 4) = Format(additionalQty * LB_Menu.List(i, 2), "0.00")
            End With
        End If
    Next i
End Sub

顺便说一句,如果您要 add/subtract 数值,请确保列表框中的所有列都已初始化为值。如果它们只是 Null,您将收到 Could not set the List property 错误。