所以这个Excel文件的整个范围是从其他27个外部文件一个一个地复制并粘贴到当前的Excel文件。来表示我的意思,下面是代码示例和刺激捕获图片。
1。导入子
在我的文件中,我有27个这样的潜艇。它比这个例子长。我的真实宏有179行作为总数。在这个例子中,它只有51行。
唯一会改变的是行号作为第6行VBA代码中的单词行。
Sub Import_NJ()
Dim Row As Integer, PathFileOpen As String, NameFileOpen As String,
TypeFileOpen As String, FullFileName As String, TabCopy As String, ModelFileName As String
Let Row = Worksheets("Control_Table").Cells("2", "D").Value
Let PathFileOpen = Worksheets("Control_Table").Cells(Row, "A").Text
Let NameFileOpen = Worksheets("Control_Table").Cells(Row, "B").Text
Let TypeFileOpen = Worksheets("Control_Table").Cells(Row, "C").Text
Let FullFileName = PathFileOpen & "\" & NameFileOpen & TypeFileOpen
Let TabCopy = Worksheets("Control_Table").Cells(Row, "J").Text
Let ModelFileName = Worksheets("Control_Table").Cells("10", "B").Text
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Workbooks.Open FileName:=FullFileName, UpdateLinks:=0
'Copy Income Statement
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("9", "C").Resize(5, 120).Copy 'Revenues
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("4", "AW").Resize(5, 120).PasteSpecial xlPasteValues
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("18", "C").Resize(4, 120).Copy 'Prod Costs
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("11", "AW").Resize(4, 120).PasteSpecial xlPasteValues
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("25", "C").Resize(26, 120).Copy 'Employee Related thru maintenance
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("17", "AW").Resize(26, 120).PasteSpecial xlPasteValues
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("53", "C").Resize(3, 120).Copy 'D&A
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("46", "AW").Resize(3, 120).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Workbooks(NameFileOpen).Close
Application.DisplayAlerts = True
End Sub
虽然它只显示了7个调用,但我的文件中有27个调用
Sub batch_import()
With Application
Call Import_NJ
Call Import_MD
Call Import_PA
Call Import_OKC
Call Import_CA
Call Import_HI
Call Import_IN
End With
Application.Calculation = xlCalculationAutomatic
ActiveWorkbook.Save
Application.DisplayAlerts = True
MsgBox _
("Batch loading Completed.")
End Sub
我尝试了什么:
关闭每个Sub中的自动计算,如第一个示例宏中所示。还有其他人尽可能多地申请。
我没有关闭屏幕更新,因为我的经理想看到它。
我在Patch sub结束时激活自动计算。
我认为整个过程减缓了整个过程,因为我在模块中有超过27个子。此外,工作表中还有一堆公式。
有没有办法加快宏关于打开文件并运行它?如果我需要详细说明这个问题,请告诉我。提前感谢并阅读我的问题。 :)
答案 0 :(得分:2)
你只是粘贴价值观;直接转移值并从考虑中删除剪贴板。可以在With ... End With中引用源或目标。
此,
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("9", "C").Resize(5, 120).Copy 'Revenues
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("4", "AW").Resize(5, 120).PasteSpecial xlPasteValues
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("18", "C").Resize(4, 120).Copy 'Prod Costs
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("11", "AW").Resize(4, 120).PasteSpecial xlPasteValues
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("25", "C").Resize(26, 120).Copy 'Employee Related thru maintenance
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("17", "AW").Resize(26, 120).PasteSpecial xlPasteValues
Workbooks(NameFileOpen).Worksheets("Total_Reports").Cells("53", "C").Resize(3, 120).Copy 'D&A
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("46", "AW").Resize(3, 120).PasteSpecial xlPasteValues
变为
With Workbooks(NameFileOpen).Worksheets("Total_Reports")
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("4", "AW").Resize(5, 120) = _
.Cells("9", "C").Resize(5, 120).Value2 'Revenues
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("11", "AW").Resize(4, 120) = _
.Cells("18", "C").Resize(4, 120).Value2 'Prod Costs
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("17", "AW").Resize(26, 120) = _
.Cells("25", "C").Resize(26, 120).Value2 'Employee Related thru maintenance
Workbooks(ModelFileName).Worksheets(TabCopy).Cells("46", "AW") = _
.Cells("53", "C").Resize(3, 120).Value2 'D&A
End With
如评论中所述,如果外部文件位于1Mb文件大小区域或其上方,请将它们保存为.XLSB(excel binary)以减少加载时间。
答案 1 :(得分:1)
您可以通过定义工作表对象变量来进一步阐明您的代码
这与@Jeeped发布的代码部分相同
Dim wsTR As Worksheet
Dim wsTC As Worksheet
Set wsTR = Workbooks(NameFileOpen).Worksheets("Total_Reports")
Set wsTC = Workbooks(ModelFileName).Worksheets(TabCopy)
wsTC.Cells(4, "AW").Resize(5, 120) = wsTR.Cells(9, "C").Resize(5, 120).Value2 ' Revenues
wsTC.Cells(11, "AW").Resize(4, 120) = wsTR.Cells(18, "C").Resize(4, 120).Value2 ' Prod Costs
wsTC.Cells(17, "AW").Resize(26, 120) = wsTR.Cells(25, "C").Resize(26, 120).Value2 ' Employee Related thru maintenance
wsTC.Cells(46, "AW") = wsTR.Cells(53, "C").Resize(3, 120).Value2 ' D&A