Feed results of UNIQUE function into array.

Herakles

Well-known Member
Joined
Jul 5, 2020
Messages
927
Office Version
  1. 365
Platform
  1. Windows
Whilst using this code does the job, is there a way of avoiding having to create the two formula's on the worksheet to
populate the array directly. Thanks.

VBA Code:
Public Function fncGetUnique(rngRange As Range) As Variant
Dim arr() As Variant

    Range("C2").Formula2 = "=SORT(UNIQUE(" & rngRange.Address & ",FALSE,FALSE))"
        
    Range("D2").Formula2 = "=CountA(C2#)"
    
    arr = Range("C2").Resize(Range("D2").Value, 1)
    
    fncGetUnique = arr

End Function
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, you could try like this:
VBA Code:
Public Function fncGetUnique(rngRange As Range) As Variant
    fncGetUnique = Evaluate("SORT(UNIQUE(" & rngRange.Address & ",FALSE,FALSE))")
End Function
 
Upvote 0
Solution
Hi, you could try like this:
VBA Code:
Public Function fncGetUnique(rngRange As Range) As Variant
    fncGetUnique = Evaluate("SORT(UNIQUE(" & rngRange.Address & ",FALSE,FALSE))")
End Function
Thanks FormR, I knew that there was a way.

Using Evaluate opens up many possibilities.
 
Upvote 0
Can also use Application:
VBA Code:
Public Function fncGetUnique(rngRange As Range) As Variant
    With Application
        fncGetUnique = .Sort(.Unique(Range("A2:A11")))
    End With
End Function
 
Upvote 0
Can also use Application:
VBA Code:
Public Function fncGetUnique(rngRange As Range) As Variant
    With Application
        fncGetUnique = .Sort(.Unique(Range("A2:A11")))
    End With
End Function
That works well too. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,655
Messages
6,120,760
Members
448,991
Latest member
Hanakoro

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