Deleting rows based on table headers

Cyclops755

New Member
Joined
Jul 26, 2011
Messages
31
Hey there everyone,
I'm trying to find some code that will compare the header column entries of table A, to the header row entries of table B; and delete any rows in A if an exact match isn't found in B.
Is anyone aware of how this can be done?
Thank you!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Since we're talking about headers, do you mean columns (up & down) rather than rows (side to side)?
 
Upvote 0
Both. In table A, I'm looking to compare the values in the first column (header column), to the values in the first row of table b (header row).
 
Upvote 0
Are the tables on separate sheets? It's going to need code, so it might be a good idea for you to give the names of the worksheets involved and the ranges of where the data can be found.
 
Upvote 0
yes, the tables are on separate sheets, named "Combine Build List" and "Parts List. Sorry for the late reply, I've been out of the office. Appreciate the help
 
Upvote 0
Okay, try this. I still wasn't sure which data was where, so I used my own names. You ought to be able to figure it out

Code:
Sub deleteReferences()
    Dim hdr As Worksheet, dta As Worksheet
    Set hdr = Sheets("header list") 'where the vertical list of names is
    Set dta = Sheets("datatable") 'where the full dataset with headers is
    h = 5 'header row
    With hdr
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        For r = lr To 1 Step -1 'assumes header list starts in row 1
            v = .Cells(r, "A").Value
            If dta.Rows(h).Find(v) Is Nothing Then .Rows(r).EntireRow.Delete
        Next r
    End With
End Sub
 
Upvote 0
Thanks for the code. Input my sheet names and tested, but it doesn't delete any rows, and instead simply clears almost all the names in the header column, and I get a run-time error 1004: Application or User-Defined error.

Current Code:
Code:
Sub deleteReferences()
    Dim hdr As Worksheet, dta As Worksheet
    Set hdr = Sheets("Combine Build List") 'where the vertical list of names is
    Set dta = Sheets("Parts List") 'where the full dataset with headers is
    h = 5 'header row
    With hdr
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        For r = lr To 1 Step -1 'assumes header list starts in row 1
            v = .Cells(r, "A").Value
            If dta.Rows(h).Find(v) Is Nothing Then .Rows(r).EntireRow.Delete
        Next r
    End With
End Sub

One other note, is that I eventually will need to also be able to add rows in a similar fashion to deleting them. Though that shouldn't be a hard adaptation of the "delete" code once we can get it working.
 
Upvote 0
Are your sheets set up as suggested? i.e is your header row on row 5 in the 'parts list' sheet?
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top