如何在ActiveWorkbook.Worksheets(“ Sheet1”)。AutoFilter.Sort中调用最新文件的工作表?

时间:2019-08-09 13:36:01

标签: excel vba

我有VBA代码,该代码调用文件夹中的文件。我要删除文档中的重复项。

我得到一个错误,因为文件名在代码中被称为显式。我需要调用最新文件。

这是我得到错误的地方

ActiveWorkbook.Worksheets("Target_Up_Q_Sum_RegMkt_data (1)").AutoFilter.Sort. _SortFields.Clea

我尝试使用'sheet1'和一个设置作为工作簿名称来调出文件。出现相同的错误

' portion of the code that names the latest folder
Dim wbcsv As Workbook, wbplanning As Workbook
Set wbplanning = Workbooks("Planning_tool.xlsm")
Dim wb As Workbook
Set wb = Workbooks.Open(MyPath & LatestFile)

'removing the duplicates of file need to change the name from '("Target_Up_Q_Sum_RegMkt_data (1)") to something that calls out the latest path
Cells.Select
    Selection.AutoFilter
    Range("AU1").Select
    ActiveWorkbook.Worksheets("Target_Up_Q_Sum_RegMkt_data (1)").AutoFilter.Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Target_Up_Q_Sum_RegMkt_data (1)").AutoFilter.Sort. _
        SortFields.Add2 Key:=Range("AU1:AU220936"), SortOn:=xlSortOnValues, Order _
        :=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Target_Up_Q_Sum_RegMkt_data (1)").AutoFilter. _
        Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

我的下标超出范围错误

0 个答案:

没有答案