mismatch this code for add items to listbox in userfrom

MohamedAmin

New Member
Joined
May 17, 2023
Messages
21
Office Version
  1. 2021
Platform
  1. Windows
VBA Code:
Private Sub CmdAddItem_Click() 'add item
ThisWorkbook.Activate
'====check product list =====
If Me.CbProductList.MatchFound = False Then: MsgBox "Data Error : Please select of items list ", vbCritical, "Inventory Program ": Me.CbProductList.SetFocus: Exit Sub

'===== check q as number====
If Not IsNumeric(Me.TbQuantity.Value) Then: MsgBox "Data Error: not have quantity: Please enter the quantity ", vbCritical, "Inventory Program": Me.TbQuantity.SetFocus: Exit Sub

'====== check raseed =======
If Not Me.CbInvStore.Value = Data.Range("BF7") Then GoTo 11
If Val(Me.TbQuantity.Value) + Val(Me.TbShipQuantity.Value) > Val(Me.TbRaseedNow.Value) Then
confir = MsgBox(" Quantity available is not sufficient warehouse for sale. please check inventory " & vbCrLf & "Do you want to continue ?", _
vbYesNo + vbMsgBoxLeft, "Inventory Programs ")
If confir = vbNo Then: Me.TbQuantity.Value = "": Me.TbQuantity.SetFocus: Exit Sub
End If
'===========================

11 np = Val(Me.TbNetPrice.Value) * Val(Me.TbQuantity.Value) * (Val(Me.TbMohDiscount.Value) / 100)

'On Error Resume Next
With ListBox1
Dim i As Integer
Dim b
i = .ListCount
.AddItem i
.List(i, 0) = Me.TbCode.Value
.List(i, 1) = Me.CbProductList.Value
.List(i, 2) = Me.TbProductType.Value
.List(i, 3) = Me.TbQuantity.Value
.List(i, 4) = Format(Me.TbNetPrice.Value, "0.00")
.List(i, 5) = Val(Me.TbQuantity.Value) * Val(Me.TbNetPrice.Value)
.List(i, 7) = Val(Me.TbQuantity.Value) * Val(Me.TbShipQuantity.Value)
.List(i, 8) = Me.TbProductPrice.Value

If Me.CbInvStore = "Purchase" Then
.List(i, 8) = Val(Me.TbNetPrice.Value) + Val(Me.TbShipQuantity.Value)
End If

If Not IsNumeric(Me.TbMohDiscount.Value) Then GoTo 1
.List(i, 6) = Format(Me.TbMohDiscount.Value / 100, "0.00%")

'b = 1 + (Me.TbMohDiscount / 100)

1 If b <= 0 Or b = Empty Then
.List(i, 9) = np
Else
.List(i, 9) = np / b
End If

.List(i, 9) = Val(.List(i, 5)) - np + Val(.List(i, 7))

If Me.CbInvStore = "Sales" Then
.List(i, 10) = Me.TbProductPrice.Value * Me.TbQuantity.Value (Here is the Mismatch )
.List(i, 11) = Val(.List(i, 9)) - np - Val(.List(i, 10))
End If

.List(i, 4) = Format(.List(i, 4), "$#,##0.00")
.List(i, 5) = Format(.List(i, 5), "$#,##0.00")
.List(i, 7) = Format(.List(i, 7), "$#,##0.00")
.List(i, 8) = Format(.List(i, 8), "$#,##0.00")
.List(i, 9) = Format(.List(i, 9), "$#,##0.00") 'net price
.List(i, 10) = Format(.List(i, 10), "$#,##0.00")
.List(i, 11) = Format(.List(i, 11), "$#,##0.00")

'.List(i, 10) = Val(.List(i, 3)) * Val(.List(i, 9)) 'q only * net price = total net price
'.List(i, 11) = Val(.List(i, 8)) * Val(Me.TbNetPrice.Value) 'all q+b * net price = total puplic price
'.List(i, 10) = Format(.List(i, 10), "#,##0.00")
'.List(i, 11) = Format(.List(i, 11), "#,##0.00")
.Selected(1) = True
End With

'For Total
Dim sum As Double
For Row = 1 To ListBox1.ListCount - 1
sum = sum + ListBox1.List(Row, 9) (Here is the Mismatch )
Next Row
Me.TbTotalNetPrice = sum
Me.TbTotalNetPrice = Format(Me.TbTotalNetPrice, "$#,##0.00")

'For Cost
If Me.CbInvStore = "Sales" Then
Dim sumc As Double
For Row = 1 To ListBox1.ListCount - 1
sumc = sumc + ListBox1.List(Row, 10) (Here is the Mismatch )
Next Row
Me.TbInvoCost = sumc
Me.TbInvoCost = Format(Me.TbInvoCost, "$#,##0.00")

'For Profit
Dim sump As Double
For Row = 1 To ListBox1.ListCount - 1
sump = sump + ListBox1.List(Row, 11) (Here is the Mismatch )
Next Row
Me.TbProfit = sump
Me.TbProfit = Format(Me.TbProfit, "$#,##0.00")
Me.TbProfit = Format(sum - sumc, "$#,##0.00")

End If
Me.TbCode = ""
Me.TbQuantity = ""
Me.TbBonusNet = ""
Me.TbBonusPercent = ""

Set TotalS = Profit.Range("Q8")
Me.TotalSales = Format(TotalS.Value + Me.TbTotalNetPrice.Value, "$#,##0.00")

Set Totalc = Profit.Range("R8")
Me.TotalCost = Format(Totalc.Value + Me.TbInvoCost.Value, "$#,##0.00")

Set Totalp = Profit.Range("S8")
Me.TotalProfit = Format(Totalp.Value + Me.TbProfit.Value, "$#,##0.00")

Me.LbPercentage.Value = Format(TotalProfit.Value / TotalSales.Value, "0.00%")


Me.TbCode.SetFocus


End Sub
 
Last edited by a moderator:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
And did you have a question? Or were you just sharing the fact that there is an error (somewhere?) in your code?
 
Upvote 0
I want to know how to solve this problem
 

Attachments

  • Screenshot 2023-05-17 at 8.56.19 PM.png
    Screenshot 2023-05-17 at 8.56.19 PM.png
    228.6 KB · Views: 13
  • Screenshot 2023-05-17 at 8.57.56 PM.png
    Screenshot 2023-05-17 at 8.57.56 PM.png
    213.4 KB · Views: 13
  • Screenshot 2023-05-17 at 8.58.36 PM.png
    Screenshot 2023-05-17 at 8.58.36 PM.png
    211.6 KB · Views: 12
Upvote 0
when I click Add item giving me this error but before it was working well i don't know what happened
 
Upvote 0
I have no doubt that you do, but most people tend to provide a bit more information, throw in a "please" and at the bare minimum, indicate which line the error occurred on. But that's most people. Not all.
 
Upvote 0
If it was working before but not now, then it seems like the data your trying to add to the listbox is not the kind that VBA is expecting. What line does the error occur on ? It will be highlighted when you press the debug button
 
Upvote 0
If it was working before but not now, then it seems like the data your trying to add to the listbox is not the kind that VBA is expecting. What line does the error occur on ? It will be highlighted when you press the debug button
here is the problem it does not sum the line, supposed after I added the item to the listbox calculated the line number 9
 

Attachments

  • Screenshot 2023-05-17 at 8.56.19 PM.png
    Screenshot 2023-05-17 at 8.56.19 PM.png
    228.6 KB · Views: 9
  • Screenshot 2023-05-17 at 8.57.56 PM.png
    Screenshot 2023-05-17 at 8.57.56 PM.png
    213.4 KB · Views: 8
  • Screenshot 2023-05-17 at 8.58.36 PM.png
    Screenshot 2023-05-17 at 8.58.36 PM.png
    211.6 KB · Views: 9
Upvote 0
here is the problem it does not sum the line, supposed after I added the item to the listbox calculated the line number 9
And you're getting a type mismatch error on this line... Do you happen to know what the expected value is? Because it seems like VBA is saying it can't added the value of sum together with whatever it is you're trying to access from that row/column of the listbox.

If you go to the Immediate Window, after having just pressed the debug button, and type:
VBA Code:
Debug.Print Row, Listbox1.List(Row, 9)

And press enter, what does it output?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top