如何加快在共享文件中运行的Excel宏?

时间:2020-03-03 14:59:15

标签: excel vba performance shared-libraries

我整理了一个代码,用于将多个Excel文件中的信息零碎复制到一个大型主文件中。但是,主文件存储在几乎已满的共享驱动器上。加上文件名(以及可以在其中找到文件的文件夹名)每月更改一次。我在路径和文件名的末尾都使用了“ *”通配符。由于有几个子文件夹,算法运行需要几分钟,并且工作表也冻结了几秒钟,因此实际上我没有节省任何时间,这是原始目的。也许您不知道如何加快速度?预先谢谢你!

您可以在下面找到代码

Sub OVtablecopy3()

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False

    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Copy

    Dim newest As Date
    Dim current As Date
    Dim right_file As String
    Dim rot_cnt As Integer
    rot_cnt = 1

    Dim my_path As String
    Dim file_name As String
    my_path = "\\mypath\which\Icouldnotwritefully\sinceitsconfidential\butyouget\theidea\maybe\*\"
    file_name = Dir("My_monthly changing_filename*.xlsx")

    Do While file_name <> vbNullString
        If rot_cnt = 1 Then
            newest = FileDateTime(file_name)
        End If
        If rot_cnt >= 1 Then
            current = FileDateTime(file_name)
        End If
        If DateSerial(Year(current), Month(current), Day(current)) >= _
                                                                   DateSerial(Year(newest), Month(newest), Day(newest)) Then
            newest = FileDateTime(file_name)
            right_file = file_name
        End If
        file_name = Dir()
        rot_cnt = rot_cnt + 1
    Loop

    Workbooks.Open (right_file)

    ActiveSheet.Paste

    Application.EnableEvents = True
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub

0 个答案:

没有答案