在处理大量行时优化vba代码

时间:2018-01-28 09:17:52

标签: excel vba excel-vba

祝贺所有人!我写了一个代码,允许我比较两个相同值的EXCEL工作表;这是:

Sub compare()

  Dim i As Integer
  Dim j As Integer
  Dim oldVal1 As Variant
  Dim oldVal2 As Variant
  Dim newVal1 As Variant
  Dim newVal2 As Variant
  Dim count As Integer

  Const equal = "equal"

  Dim WKB As Workbook
  Dim OldWS As Worksheet
  Dim NewWS As Worksheet
  Dim DiffWS As Worksheet

  Const OldWSName = "Sheet1"
  Const NewWSName = "Sheet2"
  Const DiffWSName = "Sheet3"

  Set WKB = ActiveWorkbook
  Set OldWS = WKB.Worksheets(OldWSName)
  Set NewWS = WKB.Worksheets(NewWSName)
  Set DiffWS = WKB.Worksheets(DiffWSName)

  Dim OldRow As Long
  Dim NewRow As Long

  Call OptimizeCode_Begin

  oldRow = OldWS.Cells(Rows.Count, 1).End(xlUp).Row
  newRow = NewWS.Cells(Rows.Count, 1).End(xlUp).Row

count = 1

For i = 2 To oldRow
    oldVal1 = OldWS.Cells(i, 1).Value
    oldVal2 = OldWS.Cells(i, 4).Value

    For j = 2 To newRow
        newVal1 = NewWS.Cells(j, 1).Value
        newVal2 = NewWS.Cells(j, 4).Value


        If (oldVal1 = newVal1) And (oldVal2 = newVal2) Then

            count = count + 1
            DiffWS.Cells(count, 1).Value = equal 
            DiffWS.Cells(count, 2).Value = oldVal1 
            DiffWS.Cells(count, 3).Value = oldVal2 
        End If
    Next j
Next i


Call OptimizeCode_End
DiffWS.Activate


'Reset variables

Set WKB = Nothing
Set OldWS = Nothing
Set NewWS = Nothing
Set DiffWS = Nothing

Application.ScreenUpdating = True
MsgBox ("Your data has been compared!")
End Sub

此代码前面是变量定义,我选择不在此处粘贴的长列表。但基本上, oldVal1 是来自 OldWS 工作表的第一个值,而 oldVal2 是来自同一工作表的第二个值。这些值将与 NewWS 工作表(第二个工作表)中的 newVal1 newVal2 进行比较。将相同的值复制到 DiffWS (第三个工作表),左侧有一个额外的列,状态为相等,因此为DiffWS.Cells(count + 1, 2).Value = oldVal1

我已经添加了以下功能来优化代码并使其在比较东部100000行的2个工作表时快速运行:

Sub OptimizeCode_Begin()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

EventState = Application.EnableEvents
Application.EnableEvents = False

PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False

End Sub

Sub OptimizeCode_End()

ActiveSheet.DisplayPageBreaks = PageBreakState
Application.EnableEvents = EventState
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

在我必须承认的少量行上执行速度更快,但是当工作表变大时它就不起作用了。我运行代码时,我的EXCEL崩溃了。

我应该知道的任何其他优化提示?因为我不知道如何做到这一点。如果没有可用的解决方案,我的代码将毫无用处,而且我现在更好地了解它并考虑除了EXCEL以外的其他方式来比较我的数据。

提前感谢您对此有所了解。

1 个答案:

答案 0 :(得分:1)

您的代码示例并未表明您是如何打开每个工作簿的。根据我的经验,几乎任何时候Excel崩溃,都是因为内存问题,而且往往是因为Excel的实例在后台打开,然后不是.Close&#39 ; d正确,或者excel对象可能不是SetNothing(可能是由于错误而重复执行代码)。

如果是这种情况,则 Ctrl + Alt + Del Task ManagerProcesses将显示多个Excel实例,最简单的修复是 重新启动 ,然后,当然,修复代码中Excel对象的处理。

如果目标是比较两个工作表,那么可能更好的问题是您尝试重新创建已存在的现有解决方案的功能,很可能甚至内置于您的Office副本。

根据您的版本,您已经安装了实用程序。

compare

例如,如果您正在运行 Office Pro Plus 2013 ,则可以使用 Microsoft电子表格比较 来运行报告差异。

compare example

更多信息:

我从Office 365订阅运行Excel 2016。我从来没有必要比较电子表格,但出于好奇,我只是:

  1. 点击 Windows Key Windows密钥

  2. 开始输入:spreadsheet compare

  3. start menu

    1. 坐下来让专业构建的分析/合并工具完成它的工作。
    2. spreadsheet compare

      如果所有其他方法都失败,还有许多其他(第三方)免费和付费实用程序可用(例如 xlCompare )。

      xlCompare