簡體   English   中英

Excel VBA匹配功能忽略更新的排序

[英]Excel VBA Match Function ignoring updated Sort

好的,我有一個通常按稀有性排序的項目列表,但是對於一個特定的動作,我需要對其進行排序,以便可以使用match函數,因此我將其設置為對match函數進行排序,然后再找回來。 但是,match函數將拉高排序之前的行。 有誰知道如何解決? 下面的代碼(我包括龐大的排序代碼):

'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

我對匹配功能不熟悉。 所以我無法發揮作用。 我對您原始子代碼中的所有“選擇”代碼感到困惑。 因此,我簡單地將范圍復制到數組數組中,對范圍進行排序,繞過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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM