2017-02-04 43 views
0

好吧,所以我有一個通常按稀有排序的項目列表,但對於一個特定的操作,我需要對它進行排序,以便匹配函數可以對其進行排序,所以我設置了它直到爲匹配函數進行排序,然後將其恢復。然而,匹配函數正在提取排序之前的行。有誰知道這可能會如何解決?代碼如下(我包括龐大的排序代碼):Excel VBA匹配函數忽略更新排序

'Sort for Match Function 
Sheets("Item List").Select 
    Range("L2").Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    Range("L2:U2").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    ActiveWorkbook.Worksheets("Item List").Sort.SortFields.Clear 
    ActiveWorkbook.Worksheets("Item List").Sort.SortFields.Add Key:=Range(_ 
     "L2:L300"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ 
     xlSortNormal 
    With ActiveWorkbook.Worksheets("Item List").Sort 
     .SetRange Range("L1:U300") 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 


Dim x As Integer 
x = Application.Match(BuyItem.Value, Range("TraderItems")) 
MsgBox (x) 


'Sort back to Rarity 
Sheets("Item List").Select 
    Range("L2").Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    Range("L2:U2").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    ActiveWorkbook.Worksheets("Item List").Sort.SortFields.Clear 
    ActiveWorkbook.Worksheets("Item List").Sort.SortFields.Add Key:=Range(_ 
     "R2:R300"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ 
     xlSortNormal 
    With ActiveWorkbook.Worksheets("Item List").Sort 
     .SetRange Range("L1:U300") 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
+0

替換'實際範圍TraderItems'。 –

+0

什麼是Range(「TraderItems」))?你有一個名爲範圍「TraderItems」?你在哪裏設置它? –

+0

此外,如果您已經計算過,請嘗試在排序後在其中放置強制計算範圍... – TheSilkCode

回答

0

我不熟悉Match功能。所以我無法讓這部分工作。我對你原來的子文件中的所有「選擇」代碼感到困惑。所以我簡單地將範圍複製到數組數組中,對範圍進行排序,繞過Match命令,因爲我不知道您使用的值,然後將原始數字複製回範圍。

我希望這有助於

Sub tester() 

Dim Arr() As Variant, x As Integer, arr2Rng As Range 
ActiveWorkbook.Worksheets("Item List").Activate 

    Arr = Range("L1:U300") 
    ActiveWorkbook.Worksheets("Item List").Range("L1:U300").Select 

    ActiveWorkbook.Worksheets("Item List").Sort.SortFields.Clear 
    ActiveWorkbook.Worksheets("Item List").Sort.SortFields.Add Key:=Range(_ 
     "L2:L300"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ 
     xlSortNormal 
    With ActiveWorkbook.Worksheets("Item List").Sort 
     .SetRange Range("L1:U300") 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 

    x = Application.WorksheetFunction.Match(BuyItem.Value, Range("TraderItems")) 
    MsgBox (x) 


    Range("L1").Select 
    Set arr2Rng = Range("L1") 
    arr2Rng.Resize(UBound(Arr, 1), UBound(Arr, 2)).Value = Arr 


    End Sub