Use VBA to get unique values

cdhoughton

Board Regular
Joined
Dec 5, 2003
Messages
170
Hi all

I have a number of values in the column E:E, e.g.

8882150
8882150
8882211
8882150
3022014
8882211
3022014
...

I want to use VBA to capture each value in the list once and ignore other duplicates and then assign the value to a variable, ie get the following

Emp1 = 8882150
Emp2 = 8882211
Emp3 = 3022014

I'm not sure how to do this efficiently without a big For loop and a big Or statement - can anyone advise how best to go about this?

Cheers

Chris
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
1) AdvancedFilter to get Unique Values (need header)
2) Place formula = "Emp"&Row() column next to it
3) convert formula to value

so no loop needed..
 
Upvote 0
Hi jindon

I'm afraid I've not used AdvancedFilter before - any tips on how to code this in practice?

Cheers

Chris
 
Upvote 0
Something like this..
Code:
Sub Sample()
With Sheets("Sheet1") '<- change to suite
      .Range("e1",.Range("e" & Rows.Count).End(xlUp)).AdvancedFilter _
         Action:=xlFilterCopy, CopyToRange:=.Range("g1"),Unique:=True
      With .Range("g2",.Range("g" & Rows.Count).End(xlUp)).Offset(,-1)
             .Formula= "=""Emp""&Row()-1"
             .Value = .Value
      End With
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,423
Messages
6,078,443
Members
446,338
Latest member
AliB

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