VBA to search for column by header name and remove duplicates

kenchristensen11

Board Regular
Joined
Oct 12, 2016
Messages
52
Hello,

I have the existing code, and it basically copies and pastes values from a data table and then keeps all the columns I need by header name. I have duplicates removed based on Column 3, but sometimes the original data is not sorted the same. Is there a code where it searches by column header (in this case, "EMP_SSN"), then removes duplicates from that column only?

Thanks
Code:
Sheets("TV").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select
    Sheets("Sheet3").Select
    Sheets("Sheet3").Name = "Working File"
    Range("A1").Select
 
Do Until ActiveCell.Value = ""


    If ActiveCell.Value = "PROD_NAME" _
        Or ActiveCell.Value = "EMP_NAME" _
        Or ActiveCell.Value = "EMP_SSN" _
        Or ActiveCell.Value = "CORP_NAME" _
        Or ActiveCell.Value = "CORP_TAX_ID" _
        Or ActiveCell.Value = "AFFILIATION_ID" _
        Or ActiveCell.Value = "JOB_TITLE" _
        Or ActiveCell.Value = "ADDR1" _
        Or ActiveCell.Value = "ADDR2" _
        Or ActiveCell.Value = "CITY" _
        Or ActiveCell.Value = "STATE" _
        Or ActiveCell.Value = "ZIP" _
        Or ActiveCell.Value = "WORK_TX_REGION" _
        Or ActiveCell.Value = "BIRTH_DATE" Then


        ActiveCell.Offset(0, 1).Select


    Else


        ActiveCell.EntireColumn.Select
        Selection.Delete Shift:=xlToLeft
        Selection.End(xlUp).Select


    End If
Loop


    Dim Lastrow As Long
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.AutoFilter
    ActiveWindow.SmallScroll Down:=-15
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Columns.AutoFit
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Range("$A$1:$N$3000").RemoveDuplicates Columns:=3, Header:=xlYes
    Range("A1").Select
 
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,216,028
Messages
6,128,395
Members
449,446
Latest member
CodeCybear

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