VBA code to update today's date based on specific conditions.

amaresh achar

Board Regular
Joined
Dec 9, 2016
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hi,

Can anyone please help me with VBA code to perform below task:

1. Check the existing value of cell R5.
2. If R5 is empty, then update today's date in dd-mmm-yyyy format.
3. If R5 has a valid date in dd-mmm-yy or dd-mmm-yyyy format, then DO NOT update today's date.
4. If R5 has anything except a valid date, then update today's date in dd-mmm-yyyy format.

Thanks in Advance.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try the following:
VBA Code:
Option Explicit
Sub GetDateIf()
    Dim ws As Worksheet, r As Range
    Set ws = Worksheets("Sheet1")       '<~~ *** Change to actual sheet name ***
    Set r = ws.Range("R5")
    
    If Not IsDate(r) = True And (r.NumberFormat = "dd-mmm-yy" Or r.NumberFormat = "dd-mmm-yyyy") Then
        r = Date
        r.NumberFormat = "dd-mmm-yyyy"
    End If
End Sub
 
Upvote 0
Try the following:
VBA Code:
Option Explicit
Sub GetDateIf()
    Dim ws As Worksheet, r As Range
    Set ws = Worksheets("Sheet1")       '<~~ *** Change to actual sheet name ***
    Set r = ws.Range("R5")
   
    If Not IsDate(r) = True And (r.NumberFormat = "dd-mmm-yy" Or r.NumberFormat = "dd-mmm-yyyy") Then
        r = Date
        r.NumberFormat = "dd-mmm-yyyy"
    End If
End Sub
Sorry Sir, I tried, but didn't work.
 
Upvote 0
Sorry Sir, I tried, but didn't work.
I'm afraid "didn't work" tells me very little. Did it not change the value in R5? Did it give an incorrect result? Did Excel freeze/crash? Perhaps you could share your workbook via Dropbox, Google Drive or similar file sharing platform? Or at the very least provide a copy of your worksheet using the XL2BB add in?
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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