Hello barim,
Thanks for posting the workbooks. It was huge help.
I have written and tested both of the solutions shown. One uses more advanced VBA code and the other very simple code. It provides a good contrast in what you can do with VBA.
What is really interesting about the two, is the simpler version is the slowest.
Robust VBA Solution
Sub MyLookup_1()
' The House of Brick.
' More advanced code, very fast, and handles errors.
' Define variables
Dim AltId As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim lrow1 As Long
Dim lrow2 As Long
Dim c1 As Range
Dim c2 As Range
Dim Rng1 As Range
Dim Rng2 As Range
Dim VendorIDs As Object
' Assign the workbook variables and open the workbooks if needed
On Error Resume Next
Set wb1 = Workbooks("MyDatabase.xlsx")
If Err = 9 Then
Set wb1 = Workbooks("MyDatabase.xlsx")
Err.Clear
ElseIf Err <> 0 Then
GoTo ErrorHandler
End If
Set wb2 = Workbooks("WorkingFile.xlsx")
If Err = 9 Then
Set wb2 = Workbooks("WorkingFile.xlsx")
Err.Clear
ElseIf Err <> 0 Then
GoTo ErrorHandler
End If
On Error GoTo 0
' Find the last row for each column "B" in both worksheets
lrow1 = wb1.Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
' Check for Vendor ID numbers
If lrow1 < 2 Then
MsgBox "Database Vendor ID column is empty.", vbExclamation
Exit Sub
End If
lrow2 = wb2.Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
' Check for Vendor ID numbers
If lrow2 < 2 Then
MsgBox "Working File Vendor ID column is empty", vbExclamation
Exit Sub
End If
' Assign the column "B" ranges to object variables
Set Rng1 = wb1.Sheets(1).Range("B2:B" & lrow1)
Set Rng2 = wb2.Sheets(1).Range("B2:B" & lrow2)
' Create an associative array
Set VendorIDs = CreateObject("Scripting.Dictionary")
VendorIDs.CompareMode = vbTextCompare
' Copy the unique Vendor IDs and Alternate IDs into the associative array
For Each c1 In Rng1
AltId = c1.Offset(0, 3)
If c1.Value <> "" Then
If VendorIDs.Exists(c1.Value) = False Then
VendorIDs.Add c1.Value, AltId
End If
End If
Next c1
' Check if the Vendor IDs in the Working File exist in the Database
For Each c2 In Rng2
If VendorIDs.Exists(c2.Value) = True Then
' Copy the Alternate ID to the Working File
c2.Offset(0, 3).Value = VendorIDs(c2.Value)
End If
Next c2
ErrorHandler:
If Err <> 0 Then
MsgBox "Run-time error " & "'" & Err.Number & "':" _
& vbLf & vbLf & Err.Description, vbOKOnly, "Macro - MyLookup_1"
Else
MsgBox "Copying of Alternate IDs Complete."
Rng2.Parent.Activate
Range("B2").Select
End If
End Sub
Simple VBA Solution
Sub MyLookup_2()
' The House of Straw.
' Simple code that works. But slow and does not handle errors.
' Define variables
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim lrow1 As Long
Dim lrow2 As Long
Dim c1 As Range
Dim c2 As Range
' Assign the workbook variables and open the workbooks if needed
Set wb1 = Workbooks("MyDatabase.xlsx")
Set wb2 = Workbooks("WorkingFile.xlsx")
' Find the last row for each column "B" in both worksheets
lrow1 = wb1.Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
lrow2 = wb2.Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
' Loop through column B of MyDatabase and test if there is anything matching with column B in WorkingFile
' Once found value in MyDatabase copy value from 3rd column to 3rd column on WorkingFile
For Each c1 In wb1.Sheets(1).Range("B2:B" & lrow1)
For Each c2 In wb2.Sheets(1).Range("B2:B" & lrow2)
If c1.Value = c2.Value Then
c2.Offset(0, 3).Value = c1.Offset(0, 3).Value
Exit For
End If
Next c2
Next c1
End Sub