The following two VBA modules work together. In the first on 'Sub ChangeToEExpired()' If…
- User
The following two VBA modules work together. In the first on 'Sub ChangeToE_Expired()' If ws.Cells(row, col).Value <> "" And ws.Cells(1, col).Value < DateToday Then ws.Cells(row, col).Value = "e" But in the second one 'Sub ClearExpiredLeaves()' this part of the code is not working: If eDelete And Not encounteredNonEValue Then ' Clear all 'e' values seen before this empty cell For colDelete = 2 To col - 1 If ws.Cells(row, colDelete).Value = "e" Then ws.Cells(row, colDelete).ClearContents End If The second one module 'Sub ClearExpiredLeaves()' should do the following: ' Loop through each row from 2 to 8 ' Loop through each cell in the specified column range ' If we find 'e', we mark it for potential deletion ' But If we find any value that is not 'e' (and not empty) ' Do not delete any previously found 'e's ' If we encounter an empty cell after the 'e's ' Clear all 'e' values seen before this empty cell ' Exit this column loop because we've reached an empty cell
Sub ChangeToE_Expired()
Dim ws As Worksheet Dim foundA As Boolean Dim row As Long Dim col As Long Dim DateToday As Date Set ws = ThisWorkbook.Worksheets("Leave") ws.Unprotect Password:="edit" Application.EnableEvents = False DateToday = Date ' Loop through rows from 2 to 8 For row = 2 To 8 foundA = False ' Flag to track if a non-empty cell is found ' Loop through columns from B to CP For col = 2 To 94 If Not IsEmpty(ws.Cells(row, col).Value) And VarType(ws.Cells(row, col).Value) = vbString Then ' Check if cell is not empty and before today's date, then update to 'e' If ws.Cells(row, col).Value <> "" And ws.Cells(1, col).Value < DateToday Then ws.Cells(row, col).Value = "e" foundA = True ' Set flag if a non-empty cell was found End If End If Next col Next row Call ClearExpiredLeaves End Sub
Sub ClearExpiredLeaves()
code - Assistant
- User
This is the tricky part, when the VBA code is finding 'e', for potential deletion ' If we find any value that is not 'e' (and not empty) encounteredNonEValue = True ' Do not delete any previously found 'e's This is what I mean as shown below:
e,e,e,e,h, , then do not delete any of the 'e' because the empty space comes after the data series e,e,e,e, ,h, then delete all the 'e' because there is an empty space after the 'e' and before the other value that is not an 'e' which breaks the data series e,e,e,e,e, , then delete all the 'e' because there is an empty space after the 'e'
- Assistant
- User
In my range, i have some 'e' values that are preceeded by blank values, but the code is not deleting the 'e' values: Sub ClearExpiredLeaves() Dim ws As Worksheet Dim row As Long, col As Long Dim colDelete As Long Dim eDelete As Boolean Dim encounteredNonEValue As Boolean
code - Assistant
- User
Example Scenarios
In your understanding and explanation of scenarios, the first one is wrong When you see this row: e, e, e, , h, it won’t clear the 'e' values because after the 'e's there's a non-empty value 'h'. It should be: When you see this row: e, e, e, h, it won’t clear the 'e' values because after the 'e's there's a non-empty value 'h'. (Please note that in my range the non-empty value can be a value other than h). When you see this row: e, e, e, , h, clear the 'e' values because after the 'e's there's a empty value immediately after the series of 'e's. When you see: e, e, e, , , it will clear the 'e' values because it’s followed directly by an empty cell.
- Assistant
- User
For some reaseon the code did not delete the 'e' series found that was preceeded immediately after an empty cell
- Assistant