不同表格查找重复数据VBA

分析两个表格中对应列的重复情况。

Sub Match_Dec()
    '两个表格,表格中的某一列为对应列,查找这两列中的重复记录和差异记录。
    
    Dim ar As Long, br As Long, i As Integer, j As Integer, num As Integer
        'ar/br为行数,i为外层循环数控制,j为内层循环数控制
    Dim A_Range As Range, B_Range As Range
    Dim myFont As Font
    
    Set A_Range = Worksheets("Sheet4").UsedRange
    Set B_Range = Worksheets("Sheet5").UsedRange
    
    ar = A_Range.Rows.Count
    br = B_Range.Rows.Count
    
    num = 0
'查找重复行
    
    For i = 1 To ar
        Debug.Print "第" & i; "行"
        For j = 1 To br
            If A_Range.Cells(i, 1) = B_Range.Cells(j, 1) Then
                Debug.Print "第" & j; "行为重复行"
'                'myRange.Cells(i, 1).EntireRow.Delete shift:=xlShiftUp 'xlShiftToLeft
'
                Set myFont = A_Range.Cells(i, 1).EntireRow.Font
                'Set myFont = B_Range.Cells(j, 1).EntireRow.Font
                With myFont
                .Name = "楷体"
                .Size = 15
                .Bold = True
                .Italic = True
                .Color = RGB(255, 0, 0)
                .Strikethrough = True '水平删除线
                .Underline = xlUnderlineStyleNone 'xlUnderlineStyleSingle 'xlUnderlineStyleDouble
                .Shadow = False  '是/否无变化??
                .Subscript = False
                .Superscript = False
                '具体属性设置参看:https://docs.microsoft.com/zh-cn/office/vba/api/excel.xlpattern
                End With
                
                num = num + 1
            End If
         Next
    Next
    
    Debug.Print "共有" & num & "重复行"
End Sub
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用VBA编写一个宏来实现查找两个不同表格中的不同数据,并将他们合并的功能。下面是一个示例代码,可以作为参考: ``` Sub MergeTables() Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet Dim lastRow1 As Long, lastRow2 As Long, lastRow3 As Long Dim i As Long, j As Long, k As Long Dim foundMatch As Boolean '设置工作表 Set ws1 = ThisWorkbook.Sheets("表格1") Set ws2 = ThisWorkbook.Sheets("表格2") Set ws3 = ThisWorkbook.Sheets("合并表格") '获取表格1和表格2的最后一行 lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row lastRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row '将表格1的数据复制到合并表格中 ws1.Range("A1:E" & lastRow1).Copy ws3.Range("A1") lastRow3 = lastRow1 '查找表格2中的新数据并复制到合并表格中 For i = 2 To lastRow2 foundMatch = False For j = 2 To lastRow1 If ws2.Cells(i, 1) = ws1.Cells(j, 1) And ws2.Cells(i, 2) = ws1.Cells(j, 2) And ws2.Cells(i, 3) = ws1.Cells(j, 3) And ws2.Cells(i, 4) = ws1.Cells(j, 4) And ws2.Cells(i, 5) = ws1.Cells(j, 5) Then foundMatch = True Exit For End If Next j If Not foundMatch Then lastRow3 = lastRow3 + 1 ws2.Range("A" & i & ":E" & i).Copy ws3.Range("A" & lastRow3) End If Next i MsgBox "合并完成!" End Sub ``` 此代码假设表格1和表格2都包含5列数据,并且要合并的数据在第1列到第5列中。在运行宏之前,请将工作簿中的工作表名称更改为实际使用的名称。运行宏后,合并表格将包含表格1和表格2中的所有数据,没有重复数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值