根据另一列顺序对列进行排序

时间:2019-05-09 01:32:57

标签: excel vba

我有两列A和B,我想根据B值的顺序对A进行排序。我想做这个VBA

 *11000000      11750020
 11020150       11050010
 11000002       11520090

 i want this result;
 11750020       11750020
 11050010       11050010
 11520090       11520090
 *

enter code here
Dim SortByCol As Range
Dim ToSortCol As Range
Set SortByCol = ThisWorkBook.Worksheets("sheet1").Columns("B")
Set ToSortCol = ThisWorkBook.Worksheets("sheet1").Columns("A")

Dim SortRange As Range
Set SortRange = ThisWorkBook.Sheets("sheet1").Range("D1")
Union(SortByCol, ToSortCol).Copy SortRange
Set SortRange = SortRange.CurrentRegion
With SortRange.Parent.Sort

    .SortFields.Add Key:=SortRange.Range("A" & SortRange.Rows.Count), 
    SortOn:=xlSortOnValues, DataOption:=xlSortNormal

    .SetRange SortRange
    .Header = xlYes
    .MatchCase = True
    .Orientation = xlTopToBottom:
    '.SortMethod = xlPinYin
    .Apply
End With

SortRange.Columns(2).Copy ToSortCol

SortRange.Clear

0 个答案:

没有答案