简体   繁体   中英

Sorting Worksheet data by custom lists using Excel VBA

I want to sort a column in a sheet using values in another column(which is in another sheet).

For example, the values are in the column are "abc", "def" and "ghi". These values are in a column of the first sheet, but the values of the column are jumbled up. I want them sorted such that the values are in the order of "abc","def" and "ghi".

I tried this for example:

Sheets("Sheet1").Columns("G").Sort key1:=Sheets("Sheet2").Range("D:D"), order1:=xlAscending, Header:=xlYes.

There is an error when I do so:

The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.

Not sure what's the error. Need some help on this.

除了使用宏之外,您还可以使用以下公式对记录进行排序。

=MATCH(A1,Sheet2!A:A,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM