ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to create a list of unique value in a given order (https://www.excelbanter.com/excel-programming/430456-macro-create-list-unique-value-given-order.html)

diepvic

Macro to create a list of unique value in a given order
 

Hi,

I need to write a macro which lists down only unique value from a data table
and then, it sort the list in a given order .

My case is: I need a list of currencies from a database. Then, sort them in
the following order: USD, EUR, VND, other currencies in alphabetical order.

Anybody has an idea?

Thanks so much

joel

Macro to create a list of unique value in a given order
 

Use advance filter and sort like below.

With Sheets("Sheet1")
LastRow = .Range("A" & Rows.Count).End(xlUp).Row

.Range("A1:A" & LastRow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Sheets("Sheet2").Range("C1"), _
Unique:=True
End With

With Sheets("Sheet1")

LastRow = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A1:A" & LastRow).Sort _
key1:=.Range("A1"), _
order1:=xlAscending, _
header:=xlNo

End With


"diepvic" wrote:

Hi,

I need to write a macro which lists down only unique value from a data table
and then, it sort the list in a given order .

My case is: I need a list of currencies from a database. Then, sort them in
the following order: USD, EUR, VND, other currencies in alphabetical order.

Anybody has an idea?

Thanks so much


joel

Macro to create a list of unique value in a given order
 

Andvance filter doesn't work across two worksheets. try this instead

With Sheets("Sheet1")
LastRow = .Range("A" & Rows.Count).End(xlUp).Row

.Range("A1:A" & LastRow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("C1"), _
Unique:=True

LastRow = .Range("C" & Rows.Count).End(xlUp).Row
.Range("C1:C" & LastRow).Sort _
key1:=.Range("C1"), _
order1:=xlAscending, _
header:=xlNo

End With

"diepvic" wrote:

Hi,

I need to write a macro which lists down only unique value from a data table
and then, it sort the list in a given order .

My case is: I need a list of currencies from a database. Then, sort them in
the following order: USD, EUR, VND, other currencies in alphabetical order.

Anybody has an idea?

Thanks so much


diepvic

Macro to create a list of unique value in a given order
 

Thx Joel for your reply.

Your code helps me to get a list of unique values and sort it in an
alphabetical order.
However, the requirement is to arrange them with USD in the 1st place, then
EUR, VND and other currencies. With other currencies, I can use the
alphabetical order to sort them.
E.g: my list can be: USD, EUR, VND, CHF, DKK, GBP,

joel

Macro to create a list of unique value in a given order
 

Do you ned to create the list more than once? Just move the 3 you need at
the beginning and the others will be in alphabetical order. If you ned to
autmoted the process the simply find the 3 you need at the beginning and then
cut and paste them where they belong.

With Sheets("Sheet1")
LastRow = .Range("A" & Rows.Count).End(xlUp).Row

.Range("A1:A" & LastRow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("C1"), _
Unique:=True

LastRow = .Range("C" & Rows.Count).End(xlUp).Row
.Range("C1:C" & LastRow).Sort _
key1:=.Range("C1"), _
order1:=xlAscending, _
header:=xlNo

set c=.Range("C" & Rows.Count).find("USA",lookin:=xlvlaues,lookat:=xl whole)
c.cut
..Range("C1").Insert Shift:=xldown


set c=.Range("C" & Rows.Count).find("EUR",lookin:=xlvlaues,lookat:=xl whole)
c.cut
..Range("C2").Insert Shift:=xldown

set c=.Range("C" & Rows.Count).find("VND",lookin:=xlvlaues,lookat:=xl whole)
c.cut
..Range("C3").Insert Shift:=xldown

End With





"diepvic" wrote:

Thx Joel for your reply.

Your code helps me to get a list of unique values and sort it in an
alphabetical order.
However, the requirement is to arrange them with USD in the 1st place, then
EUR, VND and other currencies. With other currencies, I can use the
alphabetical order to sort them.
E.g: my list can be: USD, EUR, VND, CHF, DKK, GBP,



All times are GMT +1. The time now is 06:05 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com