Excel VBA - How to add a popup that takes input number and adds/subtracts to cell?

firexcelsior

New Member
Joined
Aug 11, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hello!

So I have a column in Excel titled "Number of Units" and I want a popup message to appear whenever someone presses on that column that asks the user to input a number. I want that number to be added to or subtracted from the current value in the cell (hence giving a the new value for the number of units.

Any idea? I know that I probably need to use VBA, but can't seem to know where to start..

Thanks :D
 

Attachments

  • NumberOfUnits_Excel.PNG
    NumberOfUnits_Excel.PNG
    3.1 KB · Views: 10

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this - place it in the relevant Sheet code module in VBE:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const colUnits = "D"
    Static i
    Dim cval
    With Target
        If .Column <> Columns(colUnits).Column Then GoTo ep
        If .Cells.Count > 1 Then GoTo ep
        cval = .Value
        If Not IsNumeric(cval) Then GoTo ep
        i = Application.InputBox("Provide a positive number to ADD or negative to SUBTRACT from the Number of Units", "Type a number", i, , , , , 1)
        If i = False Then GoTo ep
        If IsNumeric(i) Then
            Application.EnableEvents = False
            .Value = cval + i
            Application.EnableEvents = True
        End If
    End With
ep:
    On Error Resume Next
    cval = Null
End Sub
 
Upvote 0
Solution
Thanks
Try this - place it in the relevant Sheet code module in VBE:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const colUnits = "D"
    Static i
    Dim cval
    With Target
        If .Column <> Columns(colUnits).Column Then GoTo ep
        If .Cells.Count > 1 Then GoTo ep
        cval = .Value
        If Not IsNumeric(cval) Then GoTo ep
        i = Application.InputBox("Provide a positive number to ADD or negative to SUBTRACT from the Number of Units", "Type a number", i, , , , , 1)
        If i = False Then GoTo ep
        If IsNumeric(i) Then
            Application.EnableEvents = False
            .Value = cval + i
            Application.EnableEvents = True
        End If
    End With
ep:
    On Error Resume Next
    cval = Null
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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