|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
代码如下,供参考:
- Sub 跨薄匹配()
- Dim arr, i As Long, wb As Workbook, mFile As String, j As Long
- mFile = ThisWorkbook.Path & "\1.xlsx"
- If Dir(mFile, vbNormal + vbHidden) = "" Then MsgBox "预定文件不存在!", 16 + 0, "提醒": Exit Sub
- Set wb = GetObject(mFile) ' 后台打开
- With wb
- arr = .Sheets(1).UsedRange.Value
- .Close False
- End With
- Dim d As Object: Set d = CreateObject("Scripting.Dictionary")
- For i = 2 To UBound(arr)
- If Len(arr(i, 1)) > 0 And Not d.Exists(arr(i, 1)) Then d(arr(i, 1)) = Array(arr(i, 2), arr(i, 3), arr(i, 4), arr(i, 8))
- Next
- With Sheet1
- .Activate
- arr = .UsedRange.Value
- For i = 2 To UBound(arr)
- If Len(arr(i, 6)) > 0 Then
- If d.Exists(arr(i, 6)) Then
- For j = 0 To 3
- arr(i, j + 7) = d(arr(i, 6))(j)
- Next
- Else
- For j = 7 To 10
- arr(i, j) = Empty
- Next
- End If
- End If
- Next
- .UsedRange.Value = arr
- End With
- MsgBox "数据匹配完成!", 64 + 0, "提醒"
- End Sub
复制代码
|
|