代码如下。。。
Sub test()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
tm = Timer
Dim wb As Workbook, sht As Worksheet, sh As Worksheet
Set wb = ThisWorkbook
file = wb.Path & "\11.xlsx"
Set sht = wb.Sheets("Sheet1")
arr = sht.[a1].CurrentRegion
sht.Range(sht.[a3], sht.Cells(UBound(arr), 1)).Interior.Color = xlNone
Set d = CreateObject("scripting.dictionary")
For i = 3 To UBound(arr)
d(arr(i, 1)) = i
Next
With Workbooks.Open(file, 0)
For Each sh In .Sheets
s = sh.[j3]
If d.exists(s) Then
sht.Cells(d(s), 1).Interior.Color = vbGreen
For j = 1 To 3
sh.Range(arr(1, 1 + j)) = arr(d(s), j + 1)
Next
End If
Next
.Save
' .Close
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "共耗时:" & Format(Timer - tm, "0.0000") & " 秒!!!", 64
Set d = Nothing
End Sub
Sub 遍历填充()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long
Dim br()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Dim rn As Range
LJ = ThisWorkbook.Path & "\"
f = Dir(LJ & "11.xls*")
If f = "" Then MsgBox "找不到11文件!": End
With Sheets("sheet1")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "数据源工作表为空,请先导入数据!": End
.Range("e2").Resize(r - 1, 1).Interior.ColorIndex = 0
ar = .Range("a1").Resize(r, 5)
For i = 2 To UBound(ar)
s = ar(i, 1)
If s "" Then
d(s) = i
End If
Next i
Set wb = Workbooks.Open(LJ & f, 0)
For Each sh In wb.Worksheets
xm = sh.[j3].Value
xh = d(xm)
If xh "" Then
If rn Is Nothing Then
Set rn = .Cells(xh, 1)
Else
Set rn = Union(rn, .Cells(xh, 1))
End If
sh.[a1] = ar(xh, 2)
sh.[b5] = ar(xh, 3)
sh.[d3] = ar(xh, 4)
End If
Next sh
If Not rn Is Nothing Then rn.Interior.ColorIndex = 3
wb.Close True
End With
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
Option Explicit
Sub test()
Dim ar, i&, j&, strFileName$, strPath$, wks As Worksheet
Application.ScreenUpdating = False
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "11.xlsx"
If Dir(strFileName) = "" Then
Application.ScreenUpdating = True
MsgBox "目标文件不存在", vbCritical: Exit Sub
End If
ar = Range("A1").CurrentRegion.Value
With Workbooks.Open(strFileName)
For i = 3 To UBound(ar)
For Each wks In .Worksheets
If wks.Range("J3").Value = ar(i, 1) Then
With wks
For j = 2 To UBound(ar, 2)
.Range(ar(1, j)).Interior.Color = vbRed
.Range(ar(1, j)).Value = ar(i, j)
Next j
End With
End If
Next
Next i
.Close True
End With