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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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