Use VBA to sort within each cell and then delete duplicate values within those cells

RLloyd

New Member
Joined
Aug 14, 2008
Messages
47
I have used a "sortwithincell" formula (with VBA in the background) to alphabetize the contents of many cells. It requires the addition of a helper column, but it has worked fine for years. My needs, however, have recently changed. I need a new macro to massage the data in weekly reports, where the the data must first be alphabetized within each cell (comma delimiter) and then remove duplicate values (still comma delimiter). I choose VBA so I don't have to add one or more helper columns, as it would necessitate editing many other macros. The data is in column AE and the range varies weekly from hundreds of rows to 200K+.

Any advice is always appreciated. R. Lloyd
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Apr19
 [COLOR="Navy"]Dim[/COLOR] e [COLOR="Navy"]As[/COLOR] Variant
    [COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
    Application.ScreenUpdating = False
     [COLOR="Navy"]With[/COLOR] CreateObject("System.Collections.ArrayList")
         
    [COLOR="Navy"]Set[/COLOR] Rng = Range(Range("AE1"), Range("AE" & Rows.Count).End(xlUp))
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] e [COLOR="Navy"]In[/COLOR] Split(Trim(Dn.Value), ",")
                [COLOR="Navy"]If[/COLOR] Not .Contains(Trim(e)) [COLOR="Navy"]Then[/COLOR] .Add Trim(e)
            [COLOR="Navy"]Next[/COLOR]
            .Sort: Dn.Value = Join(.ToArray(), ", ")
            Dn.WrapText = False
            .Clear
        [COLOR="Navy"]Next[/COLOR] Dn
    [COLOR="Navy"]End[/COLOR] With
    Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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