maybe something like this:
Code:
Private Sub AddButton_Click()
Listbox1.ColumnCount = 2
Dim i As Integer, j As Integer
Dim itemFound As Boolean
Dim currentQuantity As Double
Dim previousQuantity As Double
Dim arr() As Variant
' Check if the item already exists
For i = 0 To Me.Listbox1.ListCount - 1
If Me.Listbox1.Column(0, i) = CmbProductName Then ' Assuming item name is in the first column
itemFound = True
Exit For
End If
Next i
If itemFound Then
' Item exists, update the quantity
previousQuantity = Me.Listbox1.Column(1, i) ' Assuming quantity is in the second column
currentQuantity = previousQuantity + TxtQty.Value
ReDim arr(0 To Me.Listbox1.ListCount - 1)
For j = 0 To Me.Listbox1.ListCount - 1
If j = i Then
arr(j) = Me.Listbox1.Column(0, i) & ";" & currentQuantity
Else
arr(j) = Me.Listbox1.Column(0, j) & ";" & Me.Listbox1.Column(1, j)
End If
Next
' delete previous items from listbox
With Me.Listbox1
For i = .ListCount - 1 To 0 Step -1
.RemoveItem i
Next i
End With
' re-add from array
For i = 0 To UBound(arr)
Me.Listbox1.AddItem arr(i)
Next
Else
' Item doesn't exist, add it with the given quantity
Me.Listbox1.AddItem (CmbProductName.Value & ";" & TxtQty.Value)
'Me.ListBox1.Column(1, Me.ListBox1.ListCount - 1) = TxtQty.Value 'Set quantity in second column
End If
End Sub