VBA Subtraction formula on change event

n3w2excel

New Member
Joined
Oct 13, 2017
Messages
4
I am very new to vba but I am stuck trying to get a subtraction when a cell is entered.
For example I want in column A to do =B10-G10 but I want this on any row not specific to row 10
This is mainly to count how many days between 2 dates but it will change based on when Column G or B is changed.

I have the below but it doesn't work.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range

If Target.Column = 1 Then
Cells(Target.Row, 2) - Cells(Target.Row, 6))
End If
End If
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
This is not a valid line it will also only run if column A is change which is where you want your difference. What do you want to trigger the code?
Code:
Cells(Target.Row, 2) - Cells(Target.Row, 6))

This it should be something like this.
Code:
 Cells(Target.Row, 1) = Cells(Target.Row, 2) - Cells(Target.Row, 6)
 
Upvote 0
Thanks for this.

If the date is changed in either Column B or Column G the formula will update in column A, for example in column A if I were to write a formula it would be =IF(ISBLANK(B2),"",IF(ISBLANK(2),"Awaiting Date",B2-G2)). The number would change depending on what the date is changed too but show this.
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If (Target.Column = 2 Or Target.Column = 7) And (Cells(Target.Row, 2) <> "" And Cells(Target.Row, 7) <> "") Then
    Application.EnableEvents = False
    Cells(Target.Row, 1) = Cells(Target.Row, 2) - Cells(Target.Row, 7)
    Application.EnableEvents = True
 End If

If (Target.Column = 2 Or Target.Column = 7) And (Cells(Target.Row, 2) = "" Or Cells(Target.Row, 7) = "") Then
    Cells(Target.Row, 1) = "Awaiting Date"
End If
 End Sub
 
Upvote 0
Sorry I have just noticed 1 thing this is also applying to all rows but I don't want it to only change from rows 11 down so A10 B10 and F10 are not effected. thanks
 
Upvote 0
This should work.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If (Target.Column = 2 Or Target.Column = 7) And (Cells(Target.Row, 2) <> "" And Cells(Target.Row, 7) <> "") And Target.Row > 10 Then
    Application.EnableEvents = False
    Cells(Target.Row, 1) = Cells(Target.Row, 2) - Cells(Target.Row, 7)
    Application.EnableEvents = True
 End If

If (Target.Column = 2 Or Target.Column = 7) And (Cells(Target.Row, 2) = "" Or Cells(Target.Row, 7) = "") And Target.Row > 10 Then
    Cells(Target.Row, 1) = "Awaiting Date"
End If
 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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