主要内容如下:
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