hsandeep
Well-known Member
- Joined
- Dec 6, 2008
- Messages
- 1,226
- Office Version
- 2010
- Platform
- Windows
- Mobile
I am using below code which is self-explanatory.
The code lines are not working
The If statement checks if the current time is between the values of AA4Value and AG4Value, and greater than or equal to the value of AD4Value. If this condition is met, the code ‘should’ set the formula for the range of cells from “AB5” to “AB65” to “=$AB$4”. But the code is not performing this action & leaves the range of cells from “AB5” to “AB65” as .Value
Request someone to help & rectify the code.
Thanks in advance
Rich (BB code):
Private Sub Worksheet_Calculate()
' Exit the subroutine if the current time is before 2:00:00 PM
If Time < TimeSerial(14, 0, 0) Then Exit Sub
' Temporary disable Events and ScreenUpdating not to get caught in a loop
Application.EnableEvents = False
Application.ScreenUpdating = False
' Declare a Worksheet variable and set it to the sheet named "A23"
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("A23")
' Declare a variable to hold the starting cell for the loop and set it to the first cell in the range
Dim startCell As Range
Set startCell = ws.Range("AB5")
' Declare variables to hold the values of the cells used in the conditions
Dim AA4Value As Date, AG4Value As Date, AD4Value As Date
' Assign hard-coded values to the variables
AA4Value = TimeSerial(14, 0, 0): AD4Value = TimeSerial(15, 22, 30): AG4Value = TimeSerial(15, 55, 0)
' Declare a variable to hold the current time and set it to NOW()
Dim currentTime As Date
currentTime = Now
' Declare an array to hold the hard-coded values for column AA
Dim AAVals(1 To 61) As Variant
' Declare a variable to hold the start time and set it to 2:00:00 PM
Dim startTime As Date
startTime = TimeSerial(14, 0, 0)
' Loop through each element of the array
Dim k As Long
For k = 1 To 61
' Set the element to the hard-coded value
AAVals(k) = startTime + TimeSerial(0, 0, (k - 1) * 30)
Next k
' Loop through each cell in the range and set its value if it has a formula and meets the condition
Dim cell As Range
For Each cell In ws.Range("AB5", "AB65")
If cell.HasFormula And ws.Range("AA" & cell.Row).Value <= currentTime Then
cell.Value = cell.Value
End If
Next cell
If AA4Value < currentTime And AD4Value <= currentTime And currentTime < AG4Value Then
Application.EnableEvents = False
ws.Range("AB5:AB65").Formula = "=$AB$4"
Application.EnableEvents = True
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Rich (BB code):
If AA4Value < currentTime And AD4Value <= currentTime And currentTime < AG4Value Then
ws.Range("AB5:AB65").Formula = "=$AB$4"
The If statement checks if the current time is between the values of AA4Value and AG4Value, and greater than or equal to the value of AD4Value. If this condition is met, the code ‘should’ set the formula for the range of cells from “AB5” to “AB65” to “=$AB$4”. But the code is not performing this action & leaves the range of cells from “AB5” to “AB65” as .Value
Request someone to help & rectify the code.
Thanks in advance