【问题标题】:What is best way to compare two excel sheets?比较两个excel表的最佳方法是什么?
【发布时间】:2013-10-11 06:14:15
【问题描述】:

我试图通过比较每个单元格值来比较 vba 中的两个 excel 表。有没有什么最好的方法来提高性能?

当我的 Excel 表中有超过 2000 到 3000 行时。执行大约需要 5 分钟。有没有办法优化这段代码?

Sub CompareWorksheets(WS1 As Worksheet, WS2 As Worksheet)

Dim dR As Boolean
Dim r As Long, c As Integer, m As Integer
Dim lrow1 As Long, lrow2 As Long, lrow3 As Long
Dim lcoloumn1 As Integer, lcoloumn2 As Integer,
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim dupCount As Long

With WS1.UsedRange
  lrow1 = .Rows.Count
  lcoloumn1 = .Columns.Count
End With

With ws2.UsedRange
  lrow2 = .Rows.Count
  lcoloumn2 = .Columns.Count
End With

maxR = lrow1
maxC = lcoloumn1

If maxR < lrow2 Then maxR = lrow2
If maxC < lcoloumn2 Then maxC = lcoloumn2
DiffCount = 0
lrow3 = 1

For i = 1 To maxR
  dR = True
  Application.StatusBar = "Comparing worksheets " & Format(i / maxR, "0 %") & "..."
  For r = 1 To maxR
      For c = 1 To maxC
          WS1.Select
          cf1 = ""
          cf2 = ""
          On Error Resume Next
          cf1 = WS1.Cells(i, c).FormulaLocal
          cf2 = ws2.Cells(r, c).FormulaLocal
          On Error GoTo 0
          If cf1 <> cf2 Then
              dR = False
              Exit For
          Else
              dR = True
          End If
      Next c
      If dR Then
       Exit For
      End If
   Next r
     If Not dR Then
      dupCount = dupCount + 1
      WS1.Range(WS1.Cells(i, 1), WS1.Cells(i, maxC)).Select
      Selection.Copy
      Worksheets("Sheet3").Select
      Worksheets("Sheet3").Range(Worksheets("Sheet3").Cells(lrow3, 1), Worksheets   ("Sheet3").Cells(lrow3, maxC)).Select
      Selection.PasteSpecial
      lrow3 = lrow3 + 1
      WS1.Select
      For t = 1 To maxC
          WS1.Cells(i, t).Interior.ColorIndex = 19
          WS1.Cells(i, t).Select
          Selection.Font.Bold = True
      Next t
    End If
  Next i
 End Sub

谢谢!

【问题讨论】:

  • 显示你目前拥有的代码...
  • 我也是这样做的,但我没有性能问题。也许,问题在于您的代码的结构方式而不是它的功能。你能把代码给我们看看吗?

标签: excel vba


【解决方案1】:

可能最好的方法是将每个工作表的范围值传递给一个数组。
然后迭代数组的每个元素。

Sub test2()

Dim arr1(), arr2() As Variant
Dim i, j As Long

arr1 = Sheets("Sheet1").Range("A1:D4").Value
arr2 = Sheets("Sheet2").Range("A1:D4").Value

For i = 1 To UBound(arr1, 1)
    For j = 1 To UBound(arr1, 2)
        If arr1(i, j) = arr2(i, j) Then 'do the comparison here
            'code here
        End If
    Next j
Next i

End Sub

以上代码仅用于相同的范围比较。
否则,您需要添加另一个循环。
希望这能让你开始。

更新:
下面是比较单元格公式的代码部分的等价物。

Dim arr1(), arr2() As Variant

Set WS1 = ThisWorkbook.Sheets("Sheet1")
Set WS2 = ThisWorkbook.Sheets("Sheet2")

arr1 = WS1.UsedRange.FormulaLocal
arr2 = WS1.UsedRange.FormulaLocal

lrow1 = UBound(arr1, 1)
lrow2 = UBound(arr2, 1)
lcolumn1 = UBound(arr1, 2)
lcolumn2 = UBound(arr2, 2)

maxR = lrow1
maxC = lcoloumn1

If maxR < lrow2 Then maxR = lrow2
If maxC < lcoloumn2 Then maxC = lcoloumn2

DiffCount = 0
lrow3 = 1

For i = 1 To maxR
    dR = True
    Application.StatusBar = "Comparing worksheets " & Format(i / maxR, "0 %") & "..."
    For r = 1 To maxR
        For c = 1 To maxC
            cf1 = ""
            cf2 = ""
            On Error Resume Next
            cf1 = arr1(i, c)
            cf2 = arr2(r, c)
            On Error GoTo 0
            If cf1 <> cf2 Then
                dR = False
                Exit For
            Else
                dR = True
            End If
        Next c
        If dR Then
            Exit For
        End If
    Next r
'the rest of your code goes here which i cannot comprehend.

我无法改进代码的其他部分,抱歉。
我无法想象你想要完成什么。
希望对您有所帮助。

【讨论】:

  • @L42-感谢您的回复。但是,上面的代码将用户限制在特定范围内。我们不想要这种限制。我们的代码应该检查任意数量的行和列。
  • @Vicky 是的,没问题。确定动态范围后,将其分配给数组。要在您的代码中获取maxR 和maxC,请在您的数组中使用Ubound 和Lbound。比较 12000 个细胞只需不到一秒的时间。
  • +1 表示数组。另一种方法是以编程方式将一个简单的IF 测试添加到第三张表上的相同使用范围,以识别差异。一个有用的商业替代方案是一个名为 SpreadSheet Advantage 的程序 - 这看起来在可能存在不同列或行的比较之前对齐工作表(例如,工作表顶部的单个空白行将通过比较)
猜你喜欢
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2012-03-15
  • 2022-01-01
  • 1970-01-01
相关资源
最近更新 更多