how to check if a date is old

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
I'm creating a database of a library, I created a macro to check if the expiry date of the request that the user made has already passed, if this has happened the cell value should be "Expired Term" otherwise it should be "Pending" , but when the program executes the macro it always shows the value "Pending", can someone help me?


VBA Code:
Public Sub Confirm_Data()

Dim c As Long, all_registers As Long

Application.ScreenUpdating = False

ActiveWorkbook.Sheets("Requisições").Activate

all_registers = Cells(Rows.Count, "B").End(xlUp).Row

For c = 2 To all_registers

    Select Case True
    
    Case Format(Now, "dd/mm/yyyy") < Cells(c, 8).Value
        Cells(c, 9).Value = "Prazo Expirado"
        
    Case Format(Now, "dd/mm/yyyy") >= Cells(c, 8).Value
        Cells(c, 9).Value = "Pendente"
        
    End Select
    
Next c

Application.ScreenUpdating = True
    
End Sub

Capturar.JPG
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Get rid of the "FORMAT" function in your code!
The FORMAT function converts the entry to a text entry, and then your mathematical comparisons do not work.
You want to compare a valid date to a valid date (not to text).

As long as the values are entered as valid dates, you do not need to worry about the format to do the comparison. Excel sees dates as unformatted numbers anyway (specifically, dates in Excel are stored as the number of days since 1/0/1900).

So as long as the value on your worksheet is a valid date, in VBA you can compare it to either:
Now - current date AND time
Date - just the current date
 
Upvote 0
Solution
Get rid of the "FORMAT" function in your code!
The FORMAT function converts the entry to a text entry, and then your mathematical comparisons do not work.
You want to compare a valid date to a valid date (not to text).

As long as the values are entered as valid dates, you do not need to worry about the format to do the comparison. Excel sees dates as unformatted numbers anyway (specifically, dates in Excel are stored as the number of days since 1/0/1900).

So as long as the value on your worksheet is a valid date, in VBA you can compare it to either:
Now - current date AND time
Date - just the current date
thanks man!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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