VBA: Subtract date after cell change

newtail

New Member
Joined
Aug 22, 2019
Messages
1
This is my initial code out of confusion with how Worksheet_Change works

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    With ThisWorkbook.Sheets("Home")
        If Target.Address = "$B$4" And Not IsEmpty(.Range("B4", "B1", "B2").Value) Then
        
            .Range("C1").Value = DateAdd("y", Abs(.Range("B4").Value) * -1, CDate(.Range("C2").Value))
        
        End If
    End With
End Sub

based on this table:
Code:
+---+---------------------+-----------+
|   |          A          |     B     |
+---+---------------------+-----------+
| 1 | Start Date:         | 8/21/2019 |
| 2 | End Date:           | 8/22/2019 |
| 3 |                     |           |
| 4 | Days From End Date: | 14        |
+---+---------------------+-----------+

I want to be able to subtract 14 days from the End Date value and put it to Cell B1
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about this?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$4" And Not IsEmpty(Range("B2")) And Not IsEmpty(Range("B4")) Then
        Range("B1").Value = Range("B2").Value - Target.Value
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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