簡體   English   中英

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

一旦確定選擇了哪個索引(在本例中為i ),您就可以通過使用List中的索引來引用該值。

您收到的Object Required錯誤是因為您的聲明

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

正在使用.Value作為列表項。 此項目是一個值,而不是 object。

這是您重寫的子示例。 請注意,我使用單字符變量作為循環索引(這很好),但不是有意義的值。 我以(希望)有意義的方式重命名了其他變量,以使您的代碼更具自我記錄性。

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

順便說一句,如果要添加/減去數字值,請確保列表框中的所有列都使用值初始化。 如果它們只是Null ,您將收到Could not set the List property錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM