Macro to remove duplicates

Alex11

New Member
Joined
Oct 17, 2010
Messages
29
Please help with the following Macro:

At the stroke of a key eg. (Cntl R)

Select entire column A
Sort Column A
Remove duplicates in column A
select entire column B
Sort Column B
Remove duplicates in column B
Cut all data from B1 to last non empty cell
select first empty cell in column A
Paste data into first empty cell in column A
Delete entire column B

Thank you

<colgroup><col></colgroup><tbody>
</tbody>
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
have you tried macro recorder for you actions. you can use conditional formation to highlight duplicates and then filter on that colour, select down and delete, then take the filter off and do again. your output can then be tidied
 
Upvote 0
Thank you

I have tried the Macro recorder.

I am very new at this. My google searching on how to select a range up to the last empty cell and select the last empty cell led me to this attempt:

Sub Macro21()
'
' Macro21 Macro
'
' Keyboard Shortcut: Ctrl+r
'
Windows("example.xlsx").Activate
Windows("macro sort and remove.xlsm").Activate
Columns("A:A").Select
Windows("example.xlsx").Activate
Windows("macro sort and remove.xlsm").Activate
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A:A")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With


ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:= _
xlNo

Columns("B:B").Select

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("B:B")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ActiveSheet.Range("B:B").RemoveDuplicates Columns:=1, Header:= _
xlNo

Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut

Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste

Columns("B:B").Select

Selection.Delete Shift:=xlToLeft
End Sub
 
Upvote 0
The macro I attempted is unable to copy and paste only one row from column B, because I am attempting to copy from the first to last cell of data in the column. No it copies from the first cell to the last empty cell in the sheet.

How do I fix this?
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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