Need some assistance with this script...

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
708
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
I am trying to setup a Date/Time check for a workbook.
I am not getting any errors in my logic however, I just don't think this is correct.

In each If statement, I placed a check to see if the statement "If cDate" was working correct and not getting what I think I should.
I am getting the "Date Changed" value for each statement

Hope someone can validate my logic, and help correct.
Logic & Mini Sheet below:

VBA Code:
Option Explicit

Sub ResetRow30()
    Rows("30:30").ClearContents
End Sub

Sub TimeCheck()
    Application.ScreenUpdating = True
    Dim Rng         As Range
'CurrDayR
    Set Rng = Sheets(1).Range("$D$26")
    If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) > Rng Then
        Sheets(1).Range("$D$2") = "=(TODAY()-1)+TIMEVALUE(""07:00:00"")"
        Sheets(1).Range("CurrDayR").Calculate
        Sheets(1).Range("$D$30").Value = "Date Changed"
        Set Rng = Nothing
    End If
'CurrDayV
    Set Rng = Sheets(1).Range("$F$3")
    If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) > Rng Then
        Sheets(1).Range("CurrDayV").Calculate
        Sheets(1).Range("$F$30").Value = "Date Changed"
        Set Rng = Nothing
    End If
'PrevDayR
    Set Rng = Sheets(1).Range("$H$26")
    If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) - 1 > Rng Then
        Sheets(1).Range("$H$2") = "=(TODAY()-2)+TIMEVALUE(""07:00:00"")"
        Sheets(1).Range("PrevDayR").Calculate
        Sheets(1).Range("$H$30").Value = "Date Changed"
        Set Rng = Nothing
    End If
'PrevDayV
    Set Rng = Sheets(1).Range("$J$3")
    If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) - 1 > Rng Then
        Sheets(1).Range("PrevDayV").Calculate
        Sheets(1).Range("$J$30").Value = "Date Changed"
        Set Rng = Nothing
    End If
'CurrMonthV
    Set Rng = Sheets(1).Range("$L$3")
    If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) + 1 > Rng Then
        Sheets(1).Range("$L$2") = "=EOMONTH(TODAY(),-1)+1+ TIMEVALUE(""07:00:00"")"
        Sheets(1).Range("$L$3") = "=NOW()"
        Sheets(1).Range("CurrMonthV").Calculate
        Sheets(1).Range("$L$30").Value = "Date Changed"
        Set Rng = Nothing
    End If
'PrevMonthV
    Set Rng = Sheets(1).Range("$N$3")
    If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) + 1 > Rng Then
        Sheets(1).Range("$N$2") = "=EOMONTH(TODAY(),-2)+1+ TIMEVALUE(""07:00:00"")"
        Sheets(1).Range("$N$3") = "=EOMONTH(TODAY(),-1)+1+ TIMEVALUE(""07:00:00"")"
        Sheets(1).Range("PrevMonthV").Calculate
        Sheets(1).Range("$N$30").Value = "Date Changed"
        Set Rng = Nothing
    End If
    Sheets(1).Range("TimeStamp") = "=Now()"
    Application.ScreenUpdating = True
End Sub

Cell Formulas
RangeFormula
B2,L3,F3B2=NOW()
F2,D2F2=(TODAY()-1)+TIMEVALUE("07:00:00")
J2J2=TODAY()-3+1+TIMEVALUE("07:00:00")
J3J3=TODAY()-2+1+TIMEVALUE("07:00:00")
L2,N3L2=EOMONTH(TODAY(),-1)+1+ TIMEVALUE("07:00:00")
N2N2=EOMONTH(TODAY(),-2)+1+ TIMEVALUE("07:00:00")
D3:D26D3=$D2+TIME(1,0,0)
H2H2=(TODAY()-2)+TIMEVALUE("07:00:00")
H3:H26H3=$H2+TIME(1,0,0)
Named Ranges
NameRefers ToCells
CurrDayR=TimeCheck!$D$2:$D$26D3
PrevDayR=TimeCheck!$H$2:$H$26H3
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I am getting the "Date Changed" value for each statement
That suggests the IF test is true, so no idea what this means
If cDate" was working correct and not getting what I think I should.
Did you step thru code and verify values? Can't tell what you issue is they way you describe it.
 
Upvote 0
That suggests the IF test is true, so no idea what this means

Did you step thru code and verify values? Can't tell what you issue is they way you describe it.
Thanks for replying!

The goal is to not update if cDate is NOT greater than Rng.
When I step through the logic, it doesn't step over the if statement that I believe are false, and puts the "Date Change" in row 30.
Like the "PrevDay", "PrevMonth" columns.

Maybe my test isn't right, or my code is not right but...
When I get this working the way I think it should, I will add more lines in the IF statement for calculations.
Skip the calculations if the cDate statements are false.

Hope that makes sense...

Thanks!
 
Upvote 0
Pujo

For me at least it is not clear what you are trying to accomplish. Tell us more about the logic that you are trying to apply and the desired result.

I would also suggest that you add some comments to your code to specify what is supposed to happen. Do it for each if statement. That way we can tell what each one is supposed to do. Explain what these mean: CurrDayR, CurrDayV, PrevDayR, PrevDayV, CurrMonthV, PrevMonthV

The code below is a bit tighter, which makes reading it a bit easier.

VBA Code:
Sub TimeCheck()
    
    Dim Rng As Range
    
    Application.ScreenUpdating = False
    
    With ThisWorkbook.Sheets(1)
    
        .Rows("30:30").ClearContents
        
'       CurrDayR
        Set Rng = .Range("$D$26")
        If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) > Rng Then
            .Range("$D$2").Formula = "=(TODAY()-1)+TIMEVALUE(""07:00:00"")"
            .Range("$D$30") = "Date Changed"
        End If
        
'       CurrDayV
        Set Rng = .Range("$F$3")
        If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) > Rng Then
            .Range("$F$30") = "Date Changed"
        End If
        
'       PrevDayR
        Set Rng = .Range("$H$26")
        If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) - 1 > Rng Then
            .Range("$H$2").Formula = "=(TODAY()-2)+TIMEVALUE(""07:00:00"")"
            .Range("$H$30") = "Date Changed"
        End If
        
'       PrevDayV
        Set Rng = .Range("$J$3")
        If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) - 1 > Rng Then
             .Range("$J$30") = "Date Changed"
        End If
        
'       CurrMonthV
        Set Rng = .Range("$L$3")
        If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) + 1 > Rng Then
            .Range("$L$2").Formula = "=EOMONTH(TODAY(),-1)+1+ TIMEVALUE(""07:00:00"")"
            .Range("$L$3").Formula = "=NOW()"
            .Range("$L$30") = "Date Changed"
        End If
        
'       PrevMonthV
        Set Rng = .Range("$N$3")
        If CDate(Format(Now, "d-mmm-yy hh:mm:ss")) + 1 > Rng Then
            .Range("$N$2").Formula = "=EOMONTH(TODAY(),-2)+1+ TIMEVALUE(""07:00:00"")"
            .Range("$N$3").Formula = "=EOMONTH(TODAY(),-1)+1+ TIMEVALUE(""07:00:00"")"
            .Range("$N$30") = "Date Changed"
        End If
        
        .Range("B2").Formula = "=Now()"
    
    End With  'ThisWorkbook.Sheets(1)
    
    Set Rng = Nothing
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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