Although I'm new to VBA and have much of what I need working, I've run into trouble with this final step.
The sheet is being used to paste data into, and from the pasted data, a table is being created for a particular purpose.
I need to create a macro that will eliminate all the rows in a table whose "Time" column contains a time that is later than 07:45.
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 this:
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")