Additem commandButton in listbox

johnster

New Member
Joined
May 31, 2018
Messages
43
Hello,

I have a sheet (sheet1) with products (row B) and one with prices (C).
Next i have a userform (UserForm1) with all the products and a listbox (ListBox1).

When i'm clicking on a commandbutton then i want to see my product and price near each other.

This is my code that i have so far...

Private Sub CommandButton6_Click()
Dim cell As Range
Dim rng As Range
ListBox1.ColumnCount = 2
With ThisWorkbook.Sheets("Sheet1")
Set rng = .Range("B7:C7")
End With
For Each cell In rng.Cells
With Me.ListBox1
.AddItem cell.Value
ListBox1.Column(0, ListBox1.ListCount - 1) = cell.Value
ListBox1.Column(1, ListBox1.ListCount - 1) = cell.Value
End With
Next cell
End Sub

See image for my userform
e6NqHd
 
How about
Code:
Private Sub CommandButton1_Click()
With ThisWorkbook.Sheets("Sheet1")
   Me.ListBox1.AddItem .Range("B2").Value
   Me.ListBox1.Column(1, ListBox1.ListCount - 1) = .Range("C2").Value
   Me.TextBox1.Value = CDbl(Me.TextBox1.Value) + .Range("C2").Value
End With
End Sub
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I found this code but still not working

Private Sub TextBox1_Change()
Dim MySum As Double
MySum = 0
With ListBox1
For r = 0 To .ListCount - 1
MySum = MySum + .List(r, 2)
Next r
End With
TextBox1.Value = MySum
End Sub
 
Upvote 0
Fluff,

Run-time error '13':

Type mismatch..

Me.TextBox1.Value = CDbl(Me.TextBox1.Value) + .Range("C11").Value
 
Last edited:
Upvote 0
What is the value in C11 & Textbox1, when you get the error?
 
Upvote 0
If you have 1,60. in C11 then that's the problem, as it's not a number.
 
Upvote 0
Change the value in C11 so that it is a number.
 
Upvote 0
Yes i found it, In properties of the textbox you have to fill 0 as value :).

Strange is that the sum is not correct..
 
Last edited:
Upvote 0
You haven't added this line to any of your command buttons
Code:
 Me.TextBox1.Value = CDbl(Me.TextBox1.Value) + .Range("C2").Value
Also get rid of this
Code:
Private Sub TextBox1_Change()
event
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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