Removing Duplicate Columns

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
338
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
I was wondering if there was a way to automatically remove duplicate columns like there is to do with rows?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Upvote 0
Try this on a copy of your data.

Call this procedure using the following line:

Call subDeleteDuplicateColumns(Worksheets("DuplicateColumns"))

Substitute the worksheet name as appropriate.

VBA Code:
Public Sub subDeleteDuplicateColumns(WsData As Worksheet)
Dim intColumns As Integer
Dim strColumns As String
Dim arr() As String
Dim x As Integer
    
    intColumns = WsData.Range("A1").CurrentRegion.Columns.Count
         
    WsData.Rows(2).Insert
    
    With WsData.Range("B2").Resize(1, intColumns - 1)
        .Formula = "=IF(COUNTIF($A$1:B1,B$1)>1,COLUMN()," & """""" & ")"
        .Value = .Value
    End With
        
    With WsData
        arr = Split(Application.WorksheetFunction.TextJoin(",", True, .Cells(2, 2).Resize(1, intColumns - 1)), ",")
        .Rows(2).Delete
        For x = UBound(arr) To LBound(arr) Step -1
            .Cells(1, Val(arr(x))).EntireColumn.Delete
        Next x
    End With
    
End Sub
 
Upvote 0
good morning all -
I ended up creating a new sheet and pasting & transforming to rows. Then ran remove duplicates and then redid the transforming back to columns.
I wish that Excel would just have the ability to check for duplicate columns. Just like they can sort columns as well as rows.
 
Upvote 0
Did you try using the code that I posted?

Having duplicate rows is much more likely than having duplicate columns. I'm not sure how
the latter would come about.

VBA is there to supplement what Excel provides.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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