Please write me a vba code that will do the following Go down coloumn M. where the value…
- User
Please write me a vba code that will do the following Go down coloumn M. where the value of a cell is a day, clear the duplicate day values below until a different day is found. Where the new day was found, repaeat the same process as above and clear the duplicate values below. Repeat this process until the the last data entry
- Assistant
Here is a VBA code that will perform the requested action:
Sub ClearDuplicateDays() Dim ws As Worksheet Dim lastRow As Long Dim currentRow As Long ' Set the worksheet to operate on Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your sheet name ' Find the last row in column M lastRow = ws.Cells(ws.Rows.Count, "M").End(xlUp).row ' Iterate through each row from the bottom to the second row For currentRow = lastRow To 2 Step -1 ' Check if the current day value is different from the one above If ws.Cells(currentRow, "M").Value <> ws.Cells(currentRow - 1, "M").Value Then ' Clear duplicate day values below until a different day is found ClearDuplicatesBelow ws.Cells(currentRow, "M").Value, ws, currentRow + 1 End If Next currentRow End Sub Sub ClearDuplicatesBelow(ByVal day As Date, ByVal ws As Worksheet, ByVal startRow As Long) Dim lastRow As Long Dim currentRow As Long ' Find the last row in column M lastRow = ws.Cells(ws.Rows.Count, "M").End(xlUp).row ' Iterate through each row starting from the given startRow For currentRow = startRow To lastRow ' If the cell has the same day value, clear it If ws.Cells(currentRow, "M").Value = day Then ws.Cells(currentRow, "M").ClearContents Else ' If a different day is found, exit the loop Exit For End If Next currentRow End Sub