使用ListObject列引用对表进行排序

时间:2018-05-25 11:14:24

标签: excel vba excel-vba

您好,我试图在两张不同的床单中对两张桌子进行排序,并且没有快速到达。我已经看过堆栈上的示例,他们似乎正在使用变通方法来引用静态范围作为引用实际的表和列。

我试图过滤Audit_MA_Main表格列" 6周截止日期" /表格中的H列按时间顺序排列最旧到最新 &安培; 试图对Audit_Inflight表格列进行排序"状态" / B列按字母顺序A-Z A ......下降到Z.

以下是我到目前为止可以提出如何完成的方法?

https://ipfs.io/ipfs/<replace with hash you will get after adding>

1 个答案:

答案 0 :(得分:0)

所以我玩了Macro Recorder .....我应该先做,但这可能会帮助其他人。解决我的问题如下:

Sub SortByDate()
' Tables to filter Audit_MA_Main - 6 Week Due Date  Column H  Audit_MA_Main[6 Week Due Date] Oldest to Newest
'                   Audit_Inflight - Status Column B  Audit_Inflight[Status] - A-Z
Dim ASortRange As Range, MASortRange As Range
Dim CurrentWorkbook As Workbook, HomeWorksheet As Worksheet, MAInflightSheet As Worksheet, AuditInflightSheet As Worksheet

Set CurrentWorkbook = Workbooks(ActiveWorkbook.Name)
Set HomeWorksheet = CurrentWorkbook.Sheets("Home")
Set AuditInflightSheet = CurrentWorkbook.Sheets("Audit_InFlight")
Set MAInflightSheet = CurrentWorkbook.Sheets("MA_Inflight")

Dim AuditStatusLO As ListObject
Set AuditStatusLO = AuditInflightSheet.ListObjects("Audit_Inflight")
    AuditStatusLO.Sort.SortFields.Clear
    AuditStatusLO.Sort.SortFields.Add Key:=Range("Audit_Inflight[[#All],[Status]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With AuditStatusLO.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With