excel宏匹配

主要内容如下:

Sub 匹配() '进行匹配
    
    Dim exceldata1 As Variant '存放sheet1中的数据
    Dim exceldata2 As Variant '存放最终结果
    
    Dim LB1 As Integer, UB1 As Integer
    Dim LB2 As Integer, UB2 As Integer
    Dim Bin As Boolean '标记 判断是否找到匹配 找到则退出本层循环
    Dim CharData As String
    Dim i As Integer, j As Integer

    
    Application.DisplayAlerts = False '关闭提示窗口
    Set newshe = ThisWorkbook.Worksheets(1) '本工作簿的第一个工作表
    Set result = ThisWorkbook.Worksheets(3) '存放结果表
    
    result.Activate
    '清除 最终结果中的内容
    result.Columns("A:A").Select
    Selection.ClearContents
    result.Columns("C:C").Select
    Selection.ClearContents
    result.Columns("D:D").Select
    Selection.ClearContents
    result.Columns("E:E").Select
    Selection.ClearContents
    
    exceldata1 = newshe.UsedRange.Value
    exceldata2 = result.UsedRange.Value
    
    LB1 = LBound(exceldata1, 1) '通过 数组 获取第一维 即行数
    UB1 = UBound(exceldata1, 1)
    LB2 = LBound(exceldata2, 1)
    UB2 = UBound(exceldata2, 1)
    
    For i = LB1 To UB1
        j = LB2
        Bin = True
        
        CharData = Trim(newshe.Cells(i, 2).Value)
        Do While Bin = True And j <= UB2
            If Trim(result.Cells(j, 2).Value) = CharData Then
            result.Cells(j, 1) = newshe.Cells(i, 1).Value
            result.Cells(j, 3) = newshe.Cells(i, 3).Value
            result.Cells(j, 4) = newshe.Cells(i, 4).Value
            result.Cells(j, 5) = newshe.Cells(i, 5).Value
            Bin = False
            Exit Do
            End If
            j = j + 1
        Loop
    Next i
    
    result.Activate
    '以下下进行格式调整
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.NumberFormatLocal = "yyyy-mm-dd"
    Columns("B:B").Select
    Selection.NumberFormatLocal = "00000"
    
    result.UsedRange.Select '全选
End Sub

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值