如何让两个Excel宏代码运行得更快?

pkbketx9  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(164)

我从几个代码中创建了代码。我读过我可以使用数组并关闭几个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
njthzxwz

njthzxwz1#

查找和复制行

  • 假设两个工作表都是表格格式,即一行标题,没有空行或空列,并且每个工作表上只有表格(与UsedRange相关)。
Sub UpdateMaster()
    
    ' If this code is located in any of these workbooks then you shouldn't
    ' use its name but 'ThisWorkbook':
    ' 'Set swb = ThisWorkbook', or 'Set dwb = ThisWorkbook'
    ' Also, you should out-comment the 'Close' line until you are sure
    ' that the code works as expected.
    
    ' Define constants.
     
    Const SRC_WORKBOOK As String = "ExtractFile.xlsm"
    Const SRC_WORKSHEET As String = "Sheet1"
    Const SRC_LOOKUP_COLUMN As Long = 1
    Const DST_WORKBOOK As String = "Workbook.xlsm"
    Const DST_WORKSHEET As String = "Sheet1"
    Const DST_LOOKUP_COLUMN As Long = 1
    
    Dim swb As Workbook: Set swb = Workbooks(SRC_WORKBOOK)
    Dim sws As Worksheet: Set sws = swb.Worksheets(SRC_WORKSHEET)
    
    Dim Data() As Variant, srCount As Long, cCount As Long
    
    With sws.UsedRange ' reference the source (table) range
        srCount = .Rows.Count - 1 ' use -1 to exclude headers
        cCount = .Columns.Count
        Data = .Resize(srCount).Offset(1).Value
    End With
    
    Dim dwb As Workbook: Set dwb = Workbooks(DST_WORKBOOK)
    Dim dws As Worksheet: Set dws = dwb.Worksheets(DST_WORKSHEET)
    
    ' Write the values from the dest. lookup column to the dest. lookup array.
    
    Dim lData() As Variant, dfcell As Range, drCount As Long
    
    With dws.UsedRange
        drCount = .Rows.Count - 1 ' use -1 to exclude headers
        Set dfcell = .Cells(1).Offset(drCount + 1)
        lData = .Columns(DST_LOOKUP_COLUMN).Resize(drCount).Offset(1).Value
    End With
    
    ' Write the unique values from the dest. lookup array to a dictionary.
    
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
    
    Dim dr As Long
    For dr = 1 To drCount
        dict(lData(dr, 1)) = Empty
    Next dr
    
    Erase lData
    dr = 0 ' reset for the next loop
    
    ' Write the not matching values to the top of the array.
    
    Dim sr As Long, c As Long
    
    For sr = 1 To srCount
        If Not dict.Exists(Data(sr, SRC_LOOKUP_COLUMN)) Then
            dr = dr + 1
            For c = 1 To cCount
                Data(dr, c) = Data(sr, c)
            Next c
        End If
    Next sr
    
    Set dict = Nothing
    
    If dr = 0 Then
        MsgBox "Data had already been updated.", vbExclamation
        Exit Sub
    End If
    
    ' Write the top values from the array to the destination range.

    Dim drg As Range: Set drg = dfcell.Resize(dr, cCount)
    drg.Value = Data
    
    dwb.Close SaveChanges:=True

    ' Inform.

    MsgBox "Data updated.", vbInformation

End Sub

相关问题