VBA unique cells

jakobt

Active Member
Joined
May 31, 2010
Messages
337
Have a range of cells F4:F50.

I now want to write a VBA code to enter all unique items from the range in cell:


C55 and onwards

Preferably in alphabetic order..
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi there. To solve this, I just recorded a macro to do what you described. Here it is:

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("F4:F50").Select
    Selection.Copy
    Range("C55").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    ActiveSheet.Range("$C$55:$C$101").RemoveDuplicates Columns:=1, Header:=xlNo
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C55:C101") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("C55:C101")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("C55").Select
End Sub
Note that it only works on the exact cell ranges you specified, and requires you to have the correct worksheet activated when you run it.
 
Last edited:
Upvote 0
or ... you could try this ...

Code:
Sub UniqueInSequence()
    Dim rng As Range: Set rng = Range("F4:F50")
    With Range("C55").Resize(rng.Rows.Count)
        .Value = rng.Value
        .RemoveDuplicates Columns:=1, Header:=xlNo
        .Sort Key1:=Range("C55"), Order1:=xlAscending, Header:=xlNo
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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