need to combine multiple columns into 1 column and remove duplicates

asu1057

New Member
Joined
May 12, 2016
Messages
3
Hi,

I am trying to build a macro that will add multiple columns into 1 column and remove duplicates.

I ran across an issue where if a column is blank i was getting an error so i was trying to build a loop where if it is an empty cell to delete that column and move to the next step.

I am trying to paste all the columns (column B to T) into column A

I attempted the following but am still getting errors

Dim column As Long
Dim counter As Long

column = 20
Range("T1").Select

For counter = column To 2 Step -1
If IsEmpty(Cells(1, counter)) Then
Columns(counter).Delete
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select
ActiveSheet.Paste
Columns("A:A").Select
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:= _
xlYes
Range(Cells(1, counter)).Select
Next
End


If anyone can provide help, it would be much appreciated.

Thanks.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Code:
[color=darkblue]Sub[/color] Consolidate_Columns_BT()
    [color=darkblue]Dim[/color] col       [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    [color=darkblue]For[/color] col = 20 [color=darkblue]To[/color] 2 [color=darkblue]Step[/color] -1
        [color=darkblue]If[/color] Cells(1, col) <> "" [color=darkblue]Then[/color]
            Range(Cells(1, col), Cells(Rows.Count, col).End(xlUp)).Copy
            Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteAll
            Columns(col).ClearContents
        [color=darkblue]End[/color] [color=darkblue]If[/color]
        Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    [color=darkblue]Next[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,995
Members
449,480
Latest member
yesitisasport

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