Subtract one day in date when Value selected

tobysimon

New Member
Joined
Jun 14, 2024
Messages
11
Office Version
  1. 2021
Hello VBA experts

Hoping I'd get help on below problem.

I want to subtract one day in dates range ("J3 to J8") when Value selected from drop-down Cell ("H21"). The drop down reference is from Data validation from same sheet but hidden. Drop down values "Yesterday", "Yesterday1" and "Blank" (No changes should be made when "blank" is selected).

Would be really helpful if I get vba for this action to execute. Appreciate your help.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to the Board!

Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this VBA code in the VB Editor window that pops up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range

'   Exit if cell H21 not updated
    If Intersect(Target, Range("H21")) Is Nothing Then Exit Sub
    
'   Only run if H21 is "Yesterday" or "Yesterday1"
    If Range("H21") = "Yesterday" Or Range("H21") = "Yesterday1" Then
    
'       Update cells J3:J8
        Application.EnableEvents = False
        For Each cell In Range("J3:J8")
'          Subtract one from date if cell has a value
            If cell > 0 Then cell.Value = cell.Value - 1
        Next cell
        Application.EnableEvents = True
    End If

End Sub
This will automatically subtract one from the dates in cells J3:J8 whenever H21 is changed to "Yesterday" or "Yesterday1".
 
Upvote 0
Solution
Welcome to the Board!

Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this VBA code in the VB Editor window that pops up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range

'   Exit if cell H21 not updated
    If Intersect(Target, Range("H21")) Is Nothing Then Exit Sub
   
'   Only run if H21 is "Yesterday" or "Yesterday1"
    If Range("H21") = "Yesterday" Or Range("H21") = "Yesterday1" Then
   
'       Update cells J3:J8
        Application.EnableEvents = False
        For Each cell In Range("J3:J8")
'          Subtract one from date if cell has a value
            If cell > 0 Then cell.Value = cell.Value - 1
        Next cell
        Application.EnableEvents = True
    End If

End Sub
This will automatically subtract one from the dates in cells J3:J8 whenever H21 is changed to "Yesterday" or "Yesterday1".
Thank you Joe4. It worked :)
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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