A Lookup by Worksheets
An Application.Match
Approach
Option Explicit
Sub lookupValues()
Const dFirst As Long = 2
Const sFirst As Long = 2
Dim swb As Workbook: Set swb = ThisWorkbook
'Dim WBD As Workbook: Set WBD = ThisWorkbook
Dim drg As Range
Dim dLast As Long
With WBD.Worksheets(1)
dLast = .Cells(.Rows.Count, "C").End(xlUp).Row ' because merged in 'B'
Set drg = .Cells(dFirst, "B").Resize(dLast - dFirst + 1)
End With
Dim src As Worksheet
Dim srg As Range
Dim cel As Range
Dim dMatch As Variant
Dim sMatch As Variant
Dim sLast As Long
For Each src In swb.Worksheets
sLast = src.Cells(src.Rows.Count, "C").End(xlUp).Row
Set srg = Nothing
On Error Resume Next
Set srg = src.Cells(sFirst, "B").Resize(sLast - sFirst + 1)
On Error GoTo 0
If Not srg Is Nothing Then
dMatch = Application.Match(src.Name, drg, 0)
If IsNumeric(dMatch) Then
Set cel = drg.Cells(dMatch)
Do
sMatch = Application.Match(cel.Offset(, 1).Value, srg, 0)
If IsNumeric(sMatch) Then
cel.Offset(, 2).Value _
= srg.Cells(sMatch).Offset(3, 2).Value
End If
Set cel = cel.Offset(, 1).Offset(1, -1) ' because merged
Loop Until Len(cel.Value) > 0 Or cel.Row > dLast
End If
End If
Next src
'WBD.Save
'swb.Close SaveChanges:=False
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…