Forum Discussion
Macro Plus and Minus Button Help
Hi,
I am trying to create a tablet friendly stock list for inventory.
I have made functioning buttons with the attached macros to make them add and subtract for the first line of my table.
My question is how do I make buttons for each line that references the cell on the correct line.
Thank you for any help (appologies if the macros are clunky this is my first time attempting
3 Replies
- djclementsSilver Contributor
fjltbh Another strategy you might consider is to have only one button for each command and modify the code to add or subtract one from the active cell. First, move the buttons to the header row, then freeze the top row so they are always visible when scrolling down... on the ribbon, go to View > Freeze Panes > Freeze Top Row.
Command Bar in the Header Row
Then, use something along these lines to add or subtract one from the active cell if it's within the target range (column D):
Option Explicit Private Sub AdjustValue(amount As Double) Dim ws As Worksheet, target As Range, rg As Range Set ws = ActiveSheet Set target = ws.Range("D2:D" & ws.Cells(ws.Rows.Count, 4).End(xlUp).Row + 1) Set rg = ActiveCell 'Verify the active cell is within the target range (column D) If Not Intersect(target, rg) Is Nothing Then ' verify the current value is numeric If IsNumeric(rg.Value) Then rg.Value = rg.Value + amount Else MsgBox "Non-numeric value detected", vbExclamation End If Else MsgBox "Please select a cell within column D", vbExclamation End If End Sub Sub MinusOne() Call AdjustValue(-1) End Sub Sub PlusOne() Call AdjustValue(1) End SubNote: the buttons can also be made to work with both column C and D by changing the target range from "D2:D" &... to "C2:D" &... in the code above. Cheers!
- SnowMan55Bronze Contributor
fjltbh If your buttons' increments and decrements are always integer values, I highly recommend using spin buttons instead of command buttons. For an introduction, see Microsoft documentation. Spin buttons will increment or decrement a number without having to invoke VBA code, and will automatically impose lower and upper limits to the values (although the user can overtype the cell value).
VBA code is still useful in creating and initializing the spin buttons. A better method than that documentation's sample code is shown in a series of four videos describing the process, beginning with Use Excel VBA to Create Spin Buttons
Within his code, the repetitive adding of 4 to Counter is not much of a problem, but each time that calculated value is also being converted to a string value. Better code would do the addition and conversion just once per iteration, placing the result into a String variable:Dim strRow As String '[and later, within the For loop...] strRow = CStr(Counter + 4)
You do not have to set the initial value of the spin buttons to zero (or any other constant), as was done in the fourth video. You can assign the control's Value property from a cell's value before you link to it. Overall sample code:Dim Counter As Integer Dim strRow As String For Counter = 1 To 5 ActiveSheet.Spinners.Item(1).Copy ActiveSheet.Paste strRow = CStr(Counter + 4) With Selection ' -- Set the position. .Top = Range("E" & strRow).Top .Left = Range("E" & strRow).Left ' -- Set the initial value and behavior properties. .Value = Range("D" & strRow).Value .Min = 0 .Max = 5000 'or whatever value seems appropriate .SmallChange = 1 .LinkedCell = "D" & strRow End With Next Counter
FYI, the With–End With block (With statement) allows you to make multiple references to an object's properties and/or methods without having to repeat the object reference. "Selection" is a special type of object that can contain various objects, depending on the prior action of the user or VBA code. - peiyezhuBronze Contributor
tablet friendly stock list for inventory.
Android tablet like this?