复制的宏表打开原始工作簿

时间:2021-03-25 18:40:26

标签: excel vba

我将一张工作表拆分为大约 30 个工作表(都在单独的工作簿中)。我为原始工作表编写了一个简单的宏,希望将其复制到输出工作表中。我遇到的问题是,当我在输出工作表中运行宏时,它会打开原始工作簿。有人可以帮我解决这个问题,以便在复制工作表时,宏可以在不打开其他工作簿的情况下工作吗?

这是我要复制的宏 - 它只是折叠已小计的数据,然后对其进行排序(我将这些工作簿发送给不擅长 excel 的人,并试图使其尽可能简单) :

Sub collapse_sort1()
    Dim sort_status As String
    Dim r1 As Range
    
    Set r1 = ActiveSheet.Range("L1")
    
    ActiveSheet.Outline.ShowLevels RowLevels:=2
    ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
    
    If r1.Value = "D" Then
        ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add _
            Key:=Range("L2:L3000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        r1.Value = "A"
        
    Else
    
        ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add _
            Key:=Range("L2:L3000"), SortOn:=xlSortOnValues, Order:=xlDescending, _
            DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        r1.Value = "D"
    End If
    
End Sub

0 个答案:

没有答案
相关问题