Excel VBA RemoveDuplicates not working as expected

mcwsky09

New Member
Joined
Jul 5, 2012
Messages
4
Not sure what I need to do to have permission to post attachments so I may have to do this in the body of the message.

I am using code such as the following:

Code:
Range("A1:B17").RemoveDuplicates Columns:=1

where the spreadsheet contains data in columns A and B like so:

1A
1remove
1remove
2B
2remove
2remove
3B
3remove
4C
4remove
4remove
5D
5remove
5remove
6E
6remove
6remove

<tbody>
</tbody>

The problem is that when I run the code I end up with this:

1A
1remove
2B
2remove
3B
3remove
4C
4remove
5D
5remove
6E
6remove

<tbody>
</tbody>

(the real data is more complex and has blanks instead of "remove")

But when I invoke the Remove Duplicates from the Data Menu and uncheck Column B I get what I want - like this:

1A
2B
3B
4C
5D
6E

<tbody>
</tbody>


So not sure why the VBA behavior does not match the GUI behavior.

It does seem to be an issue only with Office for Mac - I am currently using Excel for Mac 2011 version 14.4.3 (140616) and it does not have this issue with Excel for Windows 2007 version 12.0.6683.5002 SP3 MSO 12.0.6683.5000
Also tried it on Windows Office 2010 and it works as intend.

I tried it on three different Macs - all with 14.4.3 same build without success. I don't currently have any other versions loaded up to test it on.

I develop on Mac but need to maintain compatibility with both Mac and Windows versions as I have users on both.
If I cannot get this working I may have to resort to much more complex looping type code.

I have a sample workbook that I can upload (once I figure that out or reach the number of posts or whatever it takes to qualify) or can send the book to someone to look but it should not be very difficult to reproduce since it is only one line of code.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
mcwsky09,

With your raw data already sorted/grouped per you screenshot.

Sample raw data:


Excel 2007
AB
11A
21remove
31remove
42B
52remove
62remove
73B
83remove
94C
104remove
114remove
125D
135remove
145remove
156E
166remove
176remove
18
Sheet1


After the macro:


Excel 2007
AB
11A
21remove
32B
42remove
53B
63remove
74C
84remove
95D
105remove
116E
126remove
13
14
15
16
17
18
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub RemoveDuplicates()
' hiker95, 07/26/2014, ME794450
Dim r As Long, lr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
With Range("C1:C" & lr)
  .ClearContents
  .FormulaR1C1 = "=RC[-2]&RC[-1]"
  .Value = .Value
End With
For r = 1 To lr
  n = Application.CountIf(Columns(3), Cells(r, 3).Value)
  If n > 1 Then
    Range("A" & r + 1 & ":B" & r + n - 1).ClearContents
  End If
  r = r + n - 1
Next r
Range("A1:B" & lr).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
Range("C1:C" & lr).ClearContents
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the RemoveDuplicates macro.
 
Last edited:
Upvote 0
In the tests that I ran for xl2007 on an old XP frame, the Excel menu driven Remove Duplicates and the VBA RemoveDuplicates gave the same results. Both delete all but the original. Looking on the web at some other posts, I see that Mac users have difficulty using the VBA version. In fact I did not see any success stories concerning a Mac using the VBA Range.RemoveDuplicates Method.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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