VBA比较两个Excel数据的异同

2021-02-01 16:17

阅读:718

标签:http   比较   exce   bool   value   相等   sam   and   als   

代码背景:

  • 由于Excel本身无法简单的比较两个Excel数据的异同,所以用VBA编写代码的方式来实现。
  • 这里的比较条件是:数据行为单位,假设对应Sheet中没有重复数据,对应数据行的所有列的数据都相等,即为此行数据相同。
  • 这里的两个Sheet的数据行量级别大约为:50000 * 50000,数据列大约:50,对应Cell中的字符串大约100以内,中英文混合。
  • 如何在Excel中调出VBA的编写工具,请参考如下链接: https://jingyan.baidu.com/article/63f236281f17650208ab3d97.html

 

整体来说,需求非常明确,代码逻辑比较简单。

相关代码:

Sub CompareData()

    Dim i As Long
    Dim j As Long
    
    Dim fullSheetName As String
    fullSheetName = "Sheet1"
    Set fullSheet = Sheets(fullSheetName)
    Dim fullDataRange As Variant
    fullDataRange = fullSheet.Range("A1", "AN43921").CurrentRegion.Value
    Dim fullSheetRowMax As Long
    fullSheetRowMax = Range("A1", "AN43921").CurrentRegion.Rows.Count
    
    Dim partialSheetName As String
    partialSheetName = "Sheet2"
    Set partialSheet = Sheets(partialSheetName)
    Dim partialDataRange As Variant
    partialDataRange = partialSheet.Range("A1", "AN40000").CurrentRegion.Value
    Dim partialSheetRowMax As Long
    partialSheetRowMax = partialSheet.Range("A1", "AN40000").CurrentRegion.Rows.Count

    Dim columnMax As Integer
    columnMax = 40
    
    Dim columnMark As Integer
    columnMark = 42
    
    Dim sameRow As Boolean
    
    For i = 1 To fullSheetRowMax
        For j = 1 To partialSheetRowMax
            sameRow = True
            For columnIndex = 1 To columnMax
                If IsEmpty(fullDataRange(i, columnIndex)) And Not IsEmpty(partialDataRange(j, columnIndex)) Then
                    sameRow = False
                    Exit For
                End If
                If IsEmpty(partialDataRange(j, columnIndex)) And Not IsEmpty(fullDataRange(i, columnIndex)) Then
                    sameRow = False
                    Exit For
                End If
                If fullDataRange(i, columnIndex)  partialDataRange(j, columnIndex) Then
                    sameRow = False
                    Exit For
                End If
            Next columnIndex
            
            If sameRow Then
                fullSheet.Cells(i, columnMark) = 1
                Exit For
            End If
        Next j
    Next i
    
    MsgBox "Successfully!"
End Sub

 

VBA比较两个Excel数据的异同

标签:http   比较   exce   bool   value   相等   sam   and   als   

原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/12813041.html


评论


亲,登录后才可以留言!