Test for current date, If Then using VBA

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
160
Office Version
  1. 2021
Platform
  1. Windows
I have an Excel table where I enter data each day. The first column enters the current date.
If the macro finds that data for the current date has already been entered, Then I'd like it to exit the macro.
Else, continue with macro.

Here is an example of the beginning of the macro:

VBA Code:
Range("A12").End(xlDown).Select
Range("A65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select

[B]ActiveCell.Value = Date
ActiveCell.Offset(0, 3).Select[/B]

    Selection.ClearContents
GSP = InputBox("Please enter the Growth Stock Current Price.", "Growth Stock Fund Price", Default, XPos:=2880, YPos:=5760)
ActiveCell.Value = ActiveCell.Value & GSP
ActiveCell.Name = "GSP"

ActiveCell.Offset(0, 1).Select
ActiveCell.Name = "GSA"
etc.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Do you mean:

"IF Today's date already exists in column A, Then do Nothing, Else enter value?"

If so, try:

VBA Code:
Sub Macro1()

    Dim r As Range
    Dim LR As Long
    
    LR = Application.Max(12, Cells(Rows.Count, 1).End(xlUp).Row)
    Set r = Cells(12, 1).Resize(LR - 11)
    
    On Error Resume Next
    Set r = r.Find(Date)
    On Error GoTo 0
    
    If r Is Nothing Then
        With Cells(LR + 1, 1)
            .Value = Date
            With .Offset(, 3)
                .Value = InputBox("Please enter the Growth Stock Current Price.", "Growth Stock Fund Price", Default, XPos:=2880, YPos:=5760)
                .Name = "GSP"
                .Offset(, 1).Name = "GSA"
            End With
        End With
    Else
        MsgBox "Growth Stock Current Price for " & Date & " already entered", vbExclamation, "Price Already Exists"
        Set r = Nothing
    End If
    
End Sub
 
Upvote 0
Thanks Jack,
What you wrote works perfectly for the question I asked.
What I neglected to say, but "implied" it by having etc. in my post, was that there is more code to the macro, so after I wanted to check for the current date, it would continue the commands.

As a novice, I thought that when you showed me how to test for the existence of the current date, then I can simply add the remaining commands that need to run.
I guess I was wrong, AGAIN!

There are more columns where I need to enter data and create named ranges.

Thanks for your help though.
John
 
Upvote 0
You're welcome, but if you need it to do other things, more precise information tends to help!

Only you have the PC screen and spreadsheet in front of you together with your knowledge of why it exists

The rest is third party interpretation and guess work of a request read on a discussion board :)
 
Last edited:
Upvote 0
I'm trying to find an Excel macro to confirm the current date in the last cell of column A
I found this macro, but when I tested it, it always returns "No Date", even though I have the current date in the last cell of the column.

Sub macro1()

With Sheet1.Range("A1")
If Not IsDate(.Value) Then
MsgBox "No date"
ElseIf .Value = Date Then
MsgBox "Date is today"
Else
MsgBox "Date is NOT today"
End If
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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