我有两张桌子,如下:
表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
对第二个表进行排序?
答案 0 :(得分:4)
第一步是创建自定义列表。
使用自定义列表排序。
在代码中
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