Userform Listbox - Button to add +1 or -1 in same row as selected

Mathericnl

New Member
Joined
Feb 16, 2022
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I can't seem to find the solution for the following:

When I select a certain row. I want a button to add +1 to the cell inside this selection in front of the productname.
But, if value is 1. It should not be visible. Only if value is >2.

Names:
Listbox1
Sheet:
Worksheet(''ActieveB'') cells in Column B.

If I select Margaritha and press the + button. I want Cell B2 to say 2. If I press the - button, it should go back to 1.

listbox.png


excel.png




Should look like:
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I named Column B:C:D as Honderdeen and put in the Rowsource below.
.
listbox.png
Exactly, that's what I needed to know.

Please Note
-----------------------
One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your data, its layout and the overall objective for it).
 
Upvote 0
Thank you for helping me out. I had to figure out what you meant exactly haha. Now I understand and will keep this in mind for other questions.
 
Upvote 0
Replace all your code with the following.
Update the names of the commandbuttons in the code.

VBA Code:
Option Explicit

Dim sh As Worksheet, rng As Range

Private Sub CommandButton1_Click()
'+ Button
  Call UpdateQty("add")
End Sub

Private Sub CommandButton2_Click()
'- Button
  Call UpdateQty("sbt")
End Sub

Sub UpdateQty(op)
'Update the Qty, if the option is "Add" add 1, if the option is "sbt" subtract 1
  Dim n As Long, i As Long
  
  i = rng.Cells(1).Row
  n = ListBox1.ListIndex
  If n = -1 Then
    MsgBox "Select item"
    Exit Sub
  End If
  If op = "add" Then
    sh.Range("B" & n + i).Value = sh.Range("B" & n + i).Value + 1
  Else
    If sh.Range("B" & n + i).Value = 0 Then
      MsgBox "You cannot put negative values."
      Exit Sub
    End If
    sh.Range("B" & n + i).Value = sh.Range("B" & n + i).Value - 1
  End If
  
  ListBox1.ListIndex = n
End Sub

Private Sub CommandButton79_Click()
  Call AddProduct("Margaritha")   'Insert Margaritha to the sheet
End Sub

Private Sub CommandButton80_Click()
  Call AddProduct("Salami")       'Insert Salami to the sheet
End Sub

'...
'Continue with the other products...
'...

Sub AddProduct(ProductName)
'Add a product to the sheet.
  Dim f As Range
  Set f = rng.Find(ProductName, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    MsgBox "The product is already added: " & ProductName
    Exit Sub
  End If
  sh.Range("C" & Rows.Count).End(3)(2).Value = ProductName
End Sub

Private Sub CommandButton81_Click()
'Delete button
  Dim n As Long, i As Long
  
  i = rng.Cells(1).Row
  n = ListBox1.ListIndex
  If n = -1 Then
    MsgBox "Select item"
    Exit Sub
  End If
  sh.Rows(n + i).Delete
End Sub

Private Sub UserForm_Activate()
  Set sh = Sheets("ActieveB")
  Set rng = sh.Range("Honderdeen")
  ListBox1.IntegralHeight = False
End Sub
 
Upvote 0
Solution
1 question. When I press the + button. It goes from 0 to 1 to 2 etc..

Can it start from 1 instead of 0?
So when I press the + button it shows 2,3,4 etc.. But never shows 1.
 
Upvote 0
Also adding seperately should still be possible.

For example:

Margaritha
2x Margaritha

I fixed this by changing:
VBA Code:
    MsgBox "The product is already added: " & ProductName
to
VBA Code:
sh.Range("C" & Rows.Count).End(3)(2).Value = ProductName
 
Upvote 0
1 question. When I press the + button. It goes from 0 to 1 to 2 etc..

Can it start from 1 instead of 0?
So when I press the + button it shows 2,3,4 etc.. But never shows 1.
I added a line to this part of the code, so it always shows 1 in front:

This is ok for me, because when it will print to a receipt. I will exclude the 1's.

VBA Code:
Sub AddProduct(ProductName)
'Add a product to the sheet.
  Dim f As Range
  Set f = rng.Find(ProductName, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
  sh.Range("C" & Rows.Count).End(3)(2).Value = ProductName
sh.Range("B" & Rows.Count).End(3)(2).Value = 1
VBA Code:
Exit Sub
  End If
  sh.Range("C" & Rows.Count).End(3)(2).Value = ProductName
sh.Range("B" & Rows.Count).End(3)(2).Value = 1
VBA Code:
End Sub
 
Upvote 0
Can it start from 1 instead of 0?

Or on the same line:
VBA Code:
Sub AddProduct(ProductName)
'Add a product to the sheet.
  Dim f As Range
  Set f = rng.Find(ProductName, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    MsgBox "The product is already added: " & ProductName
    Exit Sub
  End If
  sh.Range("B" & Rows.Count).End(3)(2).Resize(1, 2).Value = Array(1, ProductName)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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