按Excel中另一个列表的顺序对范围进行排序

时间:2009-09-24 15:26:49

标签: excel sorting

我有两张桌子,如下:

表1

  A
1 FirstPhase
2 SecondPhase
3 ThirdPhase
4 FourthPhase

表2

  A     B
1 Item1 FirstPhase
2 Item4 FourthPhase
3 Item2 SecondPhase
4 Item3 ThirdPhase

排序后我想要达到的结果是:

  A     B
1 Item1 FirstPhase
2 Item2 SecondPhase
3 Item3 ThirdPhase
4 Item4 FourthPhase

如何根据第一个表中列B的顺序按列A对第二个表进行排序?

1 个答案:

答案 0 :(得分:4)

第一步是创建自定义列表。

  • 在Excel 2007中,单击“Office图标”
  • 选择Excel选项 - 热门 - 编辑自定义列表
  • 点击“从单元格导入列表”范围按钮
  • 选择您的数据,然后按Enter键
  • 点击导入按钮
  • 单击“确定”,然后再次单击“确定”

使用自定义列表排序。

  • 选择要排序的数据。
  • 点击“主页”标签,然后点击“排序”。过滤
  • 选择自定义排序
  • 选择要排序的列
  • 然后按下订单并选择自定义列表
  • 选择自定义列表
  • 单击“确定”,然后再次单击“确定”

在代码中

Sub MakeCustomListAndSort()

Application.AddCustomList ListArray:=Sheets("Sheet1").Range("A1:A4"), ByRow:=True
'Create the custom list

ActiveWorkbook.Worksheets("Sheet2").Range("A1:B4").Sort Key1:=Range("B1:B4"), _
 Order1:=xlAscending, Header:=xlGuess, _
 OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
 Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'Sort with latest custom list

Application.DeleteCustomList Application.CustomListCount
'Delete the latest custom list

End Sub