Can you write a VBA code that will do the following. Read from B2 downwards. If it finds…
- User
Can you write a VBA code that will do the following. Read from B2 downwards. If it finds a date with the time 00:00 and the next row down has the same date but with a time that is not 00:00 The delete the row with the same date but with a time 00:00. 19/01/2024 18:30 20/01/2024 00:00 21/01/2024 00:00 (delete this row, the next row down has same date with a time tht is not 00:00) 21/01/2024 17:30
- Assistant
Sure! Here's the VBA code that will perform the described task:
Sub DeleteRows() Dim lastRow As Long Dim i As Long lastRow = Cells(Rows.Count, "B").End(xlUp).Row ' Finds the last row in column B For i = lastRow To 3 Step -1 ' Starts from the bottom and goes up until the 3rd row If TimeValue(Cells(i, "B").Value) = 0 Then ' Check if the time is 00:00 If DateValue(Cells(i, "B").Value) = DateValue(Cells(i + 1, "B").Value) And TimeValue(Cells(i + 1, "B").Value) <> 0 Then ' Check if the next row has the same date but with a time different than 00:00 Rows(i).Delete ' Deletes the row if the conditions are met End If End If Next i End SubTo use this code, follow these steps:
- Press
Alt+F11to open the VBA editor in Excel. - Insert a new module by clicking
Insert>Module. - Copy and paste the above code into the new module.
- Press