|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub MatchWorkingHours_Array()
- Dim ws1 As Worksheet, ws2 As Worksheet
- Dim dict As Object
- Dim arr1, arr2, results()
- Dim lastRow1 As Long, lastRow2 As Long
- Dim i As Long, matchKey As String
- Dim t As Double
-
- t = Timer
- Application.ScreenUpdating = False
- Application.Calculation = xlCalculationManual
-
- ' 设置工作表对象
- Set ws1 = ThisWorkbook.Worksheets("人事H")
- Set ws2 = ThisWorkbook.Worksheets("SAPH")
-
- ' 读取表2数据到数组(A:D列)
- lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
- arr2 = ws2.Range("A3:D" & lastRow2).Value
-
- ' 创建复合键字典
- Set dict = CreateObject("Scripting.Dictionary")
- For i = 1 To UBound(arr2)
- If Not IsEmpty(arr2(i, 1)) And Not IsEmpty(arr2(i, 3)) Then
- matchKey = CStr(arr2(i, 1)) & "|" & _
- arr2(i, 3)
- dict(matchKey) = dict(matchKey) + arr2(i, 4)
- End If
- Next
-
- ' 读取表1数据到数组(A:A和D:D列)
- lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
- arr1 = ws1.Range("A3:D" & lastRow1).Value
- ReDim results(1 To UBound(arr1), 1 To 1)
-
- ' 使用数组进行匹配
- For i = 1 To UBound(arr1)
- If Not IsEmpty(arr1(i, 1)) And Not IsEmpty(arr1(i, 4)) Then
- matchKey = CStr(arr1(i, 1)) & "|" & _
- arr1(i, 4)
-
- If dict.Exists(matchKey) Then
- results(i, 1) = dict(matchKey)
- Else
- results(i, 1) = "未找到"
- End If
- Else
- results(i, 1) = "关键字段为空"
- End If
- Next
-
- ' 批量写回结果到G列
- ws1.Range("G3").Resize(UBound(results)).Value = results
-
-
-
- ' 清理资源
- Set dict = Nothing
- Application.Calculation = xlCalculationAutomatic
- Application.ScreenUpdating = True
-
- MsgBox "处理完成!共匹配 " & UBound(arr1) & " 条记录" & vbCrLf & _
- "总耗时:" & Format(Timer - t, "0.00") & " 秒", _
- vbInformation, "性能报告"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|