简体   繁体   中英

Run a subroutine if a cell's date is after today

Im trying to get some vba code to run if cell l1's date has already passed. However im am getting a run time error 91. The code i am using is as follows but something is not quite right with the variables which unfortunately despite my efforts i cannot solve.

Sheets("HAULAGE PLANNER").Activate
Dim FoundCell As Object

Set FoundCell = Range("L1").Find(what:=Date)

    If FoundCell = ("<= Today") Then

   Run.Module1.nextweek

    Else: Exit Sub
   End If
End Sub

Will move my comment to an answer.


If you are looking at a specific cell, you don't need to .find(). If Range("L1").Value <= Now() Then would suffice.

You may also need to check that L1 is in fact a date, so you would use IsDate to check.

Third point, don't use activate... you can just qualify the Sheets() for the cell reference.

Mock-up of your code with above changes (untested):

Dim DateCell As Range
Set DateCell = Sheets("HAULAGE PLANNER").Range("L1")
If Not IsDate(DateCell.Value) Then Exit Sub
If DateCell.Value <= Now() Then Application.Run("Module1.nextweek")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM