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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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