我从几个代码中创建了代码。我读过我可以使用数组并关闭几个Excel函数,使它显着更快,但我没有足够的经验来实现它。
该代码适用于较少的行数,但当我将其用于我的工作表时速度很慢,工作表有-工作簿1- 70000行,工作簿2- 30000行。
这段代码删除不需要的列,添加新列,排列它们(在导出文件中),然后移动到较慢的部分来比较两个工作簿,并将新的信息行从导出文件粘贴到主工作簿中。
UPDATED我去掉了不必要的代码部分,留下了运行得很好的部分。我必须强调的是,它没有崩溃,而是很慢,我认为这是在匹配和给出true或false的输出,或者复制和粘贴所选的行时。我如何使这两个部分更有效地工作?
Sub Update()
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim recRow As Long
Dim lastRow As Long
Dim fCell As Range
Dim i As Long
Set DstFile = Workbooks("ExtractFile.xlsm")
Set wsSource = Workbooks("ExtractFile.xlsm").Worksheets("Sheet1")
Set wsDest = Workbooks("Workbook.xlsm").Worksheets("Sheet1")
Application.ScreenUpdating = False
recRow = 1
With wsSource
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
'See if item is in Master sheet
**Set fCell = wsDest.Range("A:A").Find(what:=.Cells(i, "A").Value, LookAt:=xlWhole, MatchCase:=False)**
If Not fCell Is Nothing Then
'Record is already in master sheet
recRow = fCell.Row
Else
'Need to move this to master sheet after last found record
**.Cells(i, "A").EntireRow.Copy
wsDest.Cells(recRow + 1, "A").EntireRow.Insert
recRow = recRow + 1**
End If
Next i
End With
'Clean up
Application.CutCopyMode = False
Application.ScreenUpdating = True
DstFile.Save
DstFile.Close
End Sub
1条答案
按热度按时间njthzxwz1#
查找和复制行