Hello I have a workbook where I need to input trial data for dogs, when a dog is rehomed and the dog is deleted from the masterlist, the trial data should delete along with the dog pairing however only the Pair ids and both dogs are deleting meaning that the trial data no longer lines up with the correct pair. I am completely new to VBA and coding in general
This is the current code:
Sub DeleteRowsBasedOnNA()
Dim ws As Worksheet
Dim tbl As ListObject
Dim rng As Range
Dim cell As Range
Dim delRng As Range
' Set worksheet and table
Set ws = ThisWorkbook.Worksheets("Trial Sheet")
Set tbl = ws.ListObjects("TrialData") ' Ensure the table is named 'TrialData'
' Activate worksheet (optional)
ws.Activate
' Clear any existing filters
If tbl.AutoFilter.FilterMode Then
tbl.AutoFilter.ShowAllData
End If
' Loop through each row in the table to find #N/A in columns 2 (Dog) and 3 (Partner Dog)
For Each cell In tbl.ListColumns(2).DataBodyRange
If IsError(cell.Value) Or IsError(cell.Offset(0, 1).Value) Then
If delRng Is Nothing Then
Set delRng = cell.EntireRow
Else
Set delRng = Union(delRng, cell.EntireRow)
End If
End If
Next cell
' Delete the identified rows
If Not delRng Is Nothing Then
delRng.Delete
End If
' Clear filters and reset AutoFilter mode
If tbl.AutoFilter.FilterMode Then
tbl.AutoFilter.ShowAllData
End If
End Sub
New contributor
Becca Wilkie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.