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

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.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,950
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
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!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,950
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
:)
 

Forum statistics

Threads
1,141,060
Messages
5,704,041
Members
421,323
Latest member
Exidous

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
Top