2017-02-04 51 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