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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Are you trying to populate ListBox1 with products and prices from Sheet1 when the userform opens?
Code:
Private Sub UserForm_Initialize()

    With ListBox1
        .ColumnCount = 2
        .List = Sheets("Sheet1").Range("B7:C" & Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row).Value
    End With

End Sub
 
Upvote 0
Thx for the code, now i hav 2 columns but with all my data. I just need the data from the commandbutton.
 
Upvote 0
How about
Code:
Private Sub CommandButton6_Click()
With ThisWorkbook.Sheets("Sheet1")
   Me.ListBox1.List = .Range("B7:C7").Value
End With
End Sub
 
Upvote 0
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
End With
End Sub
 
Upvote 0
Fluffy you Hero!! :stickouttounge:.
Is there a way to count the prices in listbox column 2 and show me the outcome in a textbox?
 
Upvote 0
Is there a way to count the prices in listbox column 2 and show me the outcome in a textbox?
Probably, but not something I've ever tried.
Do you mean sum the prices rather than count them?
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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