hsandeep
Well-known Member
- Joined
- Dec 6, 2008
- Messages
- 1,226
- Office Version
- 2010
- Platform
- Windows
- Mobile
Below code (self-explanatory) has to perform 3 actions. Action #3 is being performed by the code. But Action #2 is not being performed by the code even if it meets the 2 conditions specified in the If block.
Request someone to look the code & modify it suitably so that Action #2 is performed by the code when it satisfies the 2 conditions in the If block of Action #2. The code must fill ws.Range("AB5:AB65") with the Formula "=$AB$4" BUT IT IS NOT FILLING WITH FORMULA. IT LEAVES THE ws.Range("AB5:AB65") as .Value=.Value i.e. THE RANGE HAS ONLY VALUES IN EACH CELL
Thanks in advance.
Request someone to look the code & modify it suitably so that Action #2 is performed by the code when it satisfies the 2 conditions in the If block of Action #2. The code must fill ws.Range("AB5:AB65") with the Formula "=$AB$4" BUT IT IS NOT FILLING WITH FORMULA. IT LEAVES THE ws.Range("AB5:AB65") as .Value=.Value i.e. THE RANGE HAS ONLY VALUES IN EACH CELL
Thanks in advance.
Rich (BB code):
Private Sub Worksheet_Calculate()
' 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 current time and set it to the current time
Dim CurrentTime As Date
CurrentTime = Now
' Action #1:
' Check if either condition is met and exit the subroutine early if so
If ws.Range("AA4").Value > CurrentTime Or ws.Range("AG4").Value < CurrentTime Then
Exit Sub
End If
' Action #2:
' Check if the second condition is met and set the formula for the range if so
If ws.Range("AD4").Value <= CurrentTime And Not ws.Range("AB5").HasFormula Then
ws.Range("AB5:AB65").Formula = "=$AB$4"
Exit Sub
End If
'Action #3:
' 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.Cells(cell.Row, "AA").Value <= CurrentTime Then
cell.Value = cell.Value
End If
Next cell
' Declare a variable to track if all formulas have been removed and set it to True initially
Dim allFormulasRemoved As Boolean
allFormulasRemoved = True
' 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")
' Loop until all formulas have been removed
Do
' Set allFormulasRemoved to True at the start of each iteration of the loop
allFormulasRemoved = True
' Loop through each cell in the range starting from startCell
For Each cell In ws.Range(startCell, "AB65")
' Check if the cell has a formula and meets the condition, and set its value if so
If cell.HasFormula And ws.Cells(cell.Row, "AA").Value <= CurrentTime Then
cell.Value = cell.Value
' Set allFormulasRemoved to False since a formula was found and removed
allFormulasRemoved = False
' Check if the cell does not have a formula and update startCell if so
ElseIf Not cell.HasFormula Then
Set startCell = cell.Offset(1, 0)
End If
Next cell
' Continue looping until all formulas have been removed (allFormulasRemoved is True)
Loop Until allFormulasRemoved
' Turn screen updating back on after completing the loop
Application.ScreenUpdating = True
End Sub