[英]VBA Copy Paste loop - Performance Issue
I have 2 excel files. 我有2个Excel文件。 First one is the source file “Practice_New.xlsx” and second is a mapping file “A_File.xlsx”.
第一个是源文件“ Practice_New.xlsx”,第二个是映射文件“ A_File.xlsx”。 A_File is a mapping file which contains cell reference of the source file (“Practice_New.xlsx”) to the target file (I need to create this file, say “Practice_New_Output.xlsx”).
A_File是一个映射文件,其中包含源文件(“ Practice_New.xlsx”)到目标文件(我需要创建此文件,例如“ Practice_New_Output.xlsx”)的单元格引用。 I have written the below VBA code to achieve that but it's taking huge much time to complete.
我已经编写了下面的VBA代码来实现该目标,但是要花费大量时间才能完成。 Data volume in the source excel is more than 500 rows sometime.
源excel中的数据量有时超过500行。 Can anyone please help me to tune up this code to perform better?
谁能帮助我调整此代码以使其性能更好? Also, Date values are displaying as number in the output file.
另外,日期值在输出文件中显示为数字。
Sub COPYCELL()
Dim wbk As Workbook
Dim x%
Application.DisplayAlerts = False
strParamFile = "C:\Users\rezaul.hasan\Desktop\Practice\A_FILE.xlsx"
Workbooks.Open Filename:="C:\ Important\A_FILE.xlsx"
Sheets("Sheet1").Select
TargetFilename = Range("G2").Value
SourceFilename = Range("A2").Value
SourceTabName = Range("B2").Value
Set wbkt = Workbooks.Add
wbkt.SaveAs Filename:=" C:\ Important \" & TargetFilename & ".xlsx", FileFormat:=51
wbkt.Close
strFirstFile = " C:\ Important \" & SourceFilename & ".xlsx" 'Take the source excel
strSecondFile = " C:\ Important \" & TargetFilename & ".xlsx" 'take the target excel
Set wbkM = Workbooks.Open(strParamFile)
Set sh1 = Sheets("Sheet1")
lr = Range("C" & Rows.Count).End(xlUp).Row
For x = 2 To lr
Source = sh1.Range("C" & x).Value
Target1 = sh1.Range("E" & x).Value
Target2 = sh1.Range("F" & x).Value
Set wbkS = Workbooks.Open(strFirstFile)
With wbkS.Sheets(SourceTabName)
.Range(Source).Copy
End With
Set wbk = Workbooks.Open(strSecondFile)
With wbk.Sheets("Sheet1")
.Range(Target1, Target2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
wbk.Save
wbk.Close
wbkS.Close
Next
wbkM.Close
End Sub
You just need to move the code to open and close the workbooks out of the loop. 您只需要移动代码即可打开和关闭工作簿。
Sub COPYCELL2()
Application.ScreenUpdating = False
Dim x As Long
Dim SourceTabName As String, Source As String, Target1 As String, Target2 As String
Dim MapWB As Workbook, SourceWB As Workbook, TargetWB As Workbook
Set MapWB = Workbooks.Open("C:\Users\rezaul.hasan\Desktop\Practice\A_FILE.xlsx")
With MapWB.Worksheets("Sheet1")
Set SourceWB = Workbooks.Open("C:\ Important \" & .Range("A2").Value)
Set TargetWB = Workbooks.Add
TargetWB.SaveAs Filename:="C:\ Important \" & .Range("G2").Value & ".xlsx", FileFormat:=51
SourceTabName = .Range("B2").Value
For x = 2 To .Range("C" & .Rows.Count).End(xlUp).Row
Source = .Range("C" & x).Value
Target1 = .Range("E" & x).Value
Target2 = .Range("F" & x).Value
SourceWB.Sheets(SourceTabName).Range(Source).Copy
TargetWB.Sheets("Sheet1").Range(Target1, Target2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next
End With
MapWB.Close SaveChanges:=False
SourceWB.Close SaveChanges:=False
TargetWB.Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.