VBA: Compare dates based on "Production Day"

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
710
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello all.

With my "Production Day" being 7:00 AM to 7:00 AM
I am trying to workout some code that will check the current date and time, and if between the two date/times of 7:00 AM, then calculate only the eTime
If after the 7:00 AM, then advance the start and end date/times, then do the calculations.
It seems not to be working the way I am expecting it.

Here is what I have.

VBA Code:
Sub ProductionDay()
Dim eTimeStamp As Date
Dim eStartDate As Date
Dim eEndDate As Date
Dim eTime As Range

    With Sheets("Admin")
        Range("eTimeStamp").Formula = "=Now()"

        ' If button click time is between the start date/time and _
          end date/time then calculate data only
         
        If ((eTimeStamp >= eStartDate) And (eTimeStamp <= eEndDate)) Then
            Range("eTime").Calculate 'Calculates data for the current day

        Else
       
        ' If button click time is after end date/time of 7:00 AM then insert formula into _
          cells advancing the date/time by one then calculate data
         
        If ((eTimeStamp >= eStartDate) And (eTimeStamp >= eEndDate)) Then
            Range("eStartDate").FormulaR1C1 = "=Today() + TimeValue(""07:00:00"")"
            Range("eEndDate").FormulaR1C1 = "=(Today()+1) + TimeValue(""07:00:00"")"

    'Calculate other date cells with formulas       

            Range("eTime").Calculate
        End If
       
      Sheets("6AM").Calculate
            If Not Application.CalculationState = xlDone Then
                DoEvents
            End If
        End If
    End With
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I may have figured it out but could someone check me on this please.

VBA Code:
    With Sheets("Admin")
     If DateTime.Now() >= CDate(eStartDate) And DateTime.Now() <= CDate(eEndDate) Then
        Range(eTime).Calculate
     Else
     If DateTime.Now() >= CDate(eEndDate) Then
        Range("eStartDate").Formula = "=Today() + TimeValue(""07:00:00"")"
        Range("eEndDate").Formula = "=(Today()+1) + TimeValue(""07:00:00"")"
     End If
        
      Sheets("6AM").Calculate
            If Not Application.CalculationState = xlDone Then
                DoEvents
            End If
        End If
    End With
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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