Enter a value in one cell and subtract from another without formula

wilkisa

Well-known Member
Joined
Apr 7, 2002
Messages
657
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
My user currently has a range of cells D7:D14. There are no formulas in these cells, just values. He wants to input a number in the corresponding cell in column F, hit ENTER, and have this number subtracted from the cell in column D, and display the result. He also wants the cell in column F to go blank so it can have another entry at a future date.

Example: D7 contains the number 28. He types in the number 5 in cell F7 and hits ENTER. D7 should change to number 23 and cell F7 should now be blank.

Can someone help with this? I'm sure it has to be done in code.

Thanks!
Shirlene
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("F7:F14")) Is Nothing Then Exit Sub
Application.EnableEvents = False
With Target
    .Offset(, -2).Value = .Offset(, -2).Value - .Value
    .ClearContents
End With
Application.EnableEvents = True
End Sub

then press ALT + Q to close the code window.
 
Upvote 0
Thank you so much, VoG. It works perfectly!
 
Upvote 0
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("H7:H14")) Is Nothing Then Exit Sub
Application.EnableEvents = False
With Target
    .Offset(, -3).Value = .Offset(, -3).Value - .Value
    .ClearContents
End With
Application.EnableEvents = True
End Sub

then press ALT + Q to close the code window.


Now my user has thrown a curve ball at me (or us!). He has added a second sheet to the workbook and is entering the number in column C (replaces the original column H on Sheet1). He needs the macro to match the name from Sheet2, Column A to the name on Sheet1, Column A and then subtract the value from Sheet2, Column C from Sheet1, Column E. Sheet1, Column E should always be just a number and no formula. We can eliminate Sheet1 columns F, G, & H from the sheet.

Sheet2 will be added onto throughout the month so he only needs it to subtract as he enters each line.

Can anyone help?
 
Upvote 0
Try this in Sheet2's code module. I've assumed that the value entered in column C is to be deleted - if not delete the lines in blue

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Found As Range
If Target.Column = 3 Then
    Set Found = Sheets("Sheet1").Columns("A").Find(what:=Target.Offset(, -2).Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not Found Is Nothing Then
        With Found.Offset(, 4)
            .Value = .Value - Target.Value
        End With
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0
Again, VoG, you are my hero! It works perfectly!!!!
 
Upvote 0
Okay, now my user wants another change. When he enters the value on Sheet2, he wants to be able to undo it if it is a wrong value. As it is right now, once he hits ENTER he does not have the ability to undo the change. He also is not able to see what the starting value is on Sheet1 unless he looks there first. I am also recommending that he turn on Tracked Changes.

Thanks for any help I can get.
Shirlene
 
Upvote 0
That would be difficult in VBA because once a macro runs it deletes the undo stack. There is Application.Undo but that is a bit flaky and has to be pretty much the first line in a procedure.

Say he enters 5 and then changes his mind, all he has to do is enter -5 in the same cell to undo the previous change (I think - please try it:))
 
Upvote 0
Thanks, Peter. It just didn't occur to me that it would work in reverse. Also, the tracked changes doesn't work as I would like so I going to forget that idea. I think the -value will do the job just fine.
 
Upvote 0

Forum statistics

Threads
1,215,281
Messages
6,124,045
Members
449,139
Latest member
sramesh1024

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