Macro for updating the date, in a cell... maybe

RogueRoustabout

New Member
Joined
Mar 11, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
In simple terms here is the macro I want to write:
- When "Sheet1" is activated,
- If the date in "Range1" is current, end macro
- If the date in "Range1" is not current,
- Prompt the user to update (Msg box "do you want to update the date? yes/no")
- If user clicks "no" - end macro
- If user clicks "Yes" - update "Range1" with current date.

Below is the code that I tried to put together, but I am missing something.


VBA Code:
Private Sub Worksheet_Activate()
   Range("N15:R15").Select
If Range("N15:R15").Value = Today Then
    Exit Sub
End If
response = MsgBox("Would you like to set the date to today?", vbYesNo)

If response = vbNo Then
    MsgBox ("Macro Ending")
    Exit Sub
End If
    Range("N15:R15").Select
    ActiveCell.FormulaR1C1 = Now
End Sub

Thank you for any help!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try the code below

VBA Code:
Private Sub Worksheet_Activate()
    Dim response As Integer
    
    If Range("N15").Value = Date Then
        Exit Sub
    Else
        response = MsgBox("Would you like to set the date to today?", vbYesNo)

        If response = vbNo Then
            MsgBox "Macro Ending"
            Exit Sub
        End If
        
        Range("N15") = Date
    
    End If

End Sub
 
Upvote 0
Try the code below

VBA Code:
Private Sub Worksheet_Activate()
    Dim response As Integer
   
    If Range("N15").Value = Date Then
        Exit Sub
    Else
        response = MsgBox("Would you like to set the date to today?", vbYesNo)

        If response = vbNo Then
            MsgBox "Macro Ending"
            Exit Sub
        End If
       
        Range("N15") = Date
   
    End If

End Sub
That's it! I am rusty in VBA and was unfamiliar with Date and Integers. Thank you for the quick help!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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