I'm new to VBA and have got most of what I need working but I've got stuck on this last part.
The sheet is being used to paste data into and then create a table for a specific purpose from the pasted data.
I need to write a macro that will delete the rows in a table that have a time later than 07:45 in the "Time" column.
The table is as follows:
ID | Surname | Init | Location | Time | Event | Destination |
---|---|---|---|---|---|---|
12 | Name1 | I1 | Loc1 | 18:00 | Ev1 | Dest1 |
34 | Name2 | I2 | Loc2 | 07:45 | Ev2 | Dest2 |
56 | Name3 | I3 | Loc3 | 11:00 | Ev3 | Dest3 |
78 | Name4 | I4 | Loc4 | 05:00 | Ev4 | Dest4 |
This is a screenshot of the table currently
After running the macro, it should look like:
ID | Surname | Init | Location | Time | Event | Destination |
---|---|---|---|---|---|---|
34 | Name2 | I2 | Loc2 | 07:45 | Ev2 | Dest2 |
78 | Name4 | I4 | Loc4 | 05:00 | Ev4 | Dest4 |
This is how the table should look after
I've already used this code to delete empty table rows on a different file, but I'm not sure how to adapt it to work for this purpose:
Dim EventsRng As Range
On Error Resume Next
Set EventsRng = Range("Events[[ID]]").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not EventsRng Is Nothing Then
EventsRng.Delete Shift:=xlUp
End If
I was also suggested this, but haven't been able to adapt it to work either:
Private Sub deleteTableRowsBasedOnCriteria(tbl As ListObject, columnName As String, criteria As String)
Dim x As Long, lastrow As Long, lr As ListRow
lastrow = tbl.ListRows.Count
For x = lastrow To 1 Step -1
Set lr = tbl.ListRows(x)
If Intersect(lr.Range, tbl.ListColumns(columnName).Range).Value = criteria Then
'lr.Range.Select
lr.Delete
End If
Next x
End Sub
Dim tbl As ListObject
Set tbl = ThisWorkbook.Worksheets("Sheet1").ListObjects("Events")
Call deleteTableRowsBasedOnCriteria(tbl, "Time", ">07:45")