Excel VBA- Lookup value in another sheet, return corresponding columns

jthaney10

New Member
Joined
Sep 30, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I know how to do what I need with XLOOKUP, but I'm dealing with about 16k lines of data and quite a few columns so I figured VBA would work best. Still new at getting through VBA code, so any help would be much appreciated.

I know I'll need to find the last row of each Workbook, and two for loops to get through all the values. Here's a simplified version of what I'm working with:
New MASTER.xlsx
ABCDEF
1CPN#TLN#ReasonPrecheckNameWR#
2D12341234567890
3F54320856912345
4G23236892856936
5E43211256894585
6J12346975356893
7Pull data from Old Master and place it above
New Master


Old MASTER.xlsx
ABCDEFGHIJKL
1Device #SumOfCustCountcKVAAcKVABcKVACmaxFAReasonY/NPrecheckInfoNameWR#
2G232330032301080Wire Length, Cust CountYNot found in Overall ChecklistICON: J. RODRIGUEZ10528055
3J123420300967.12Needs to be prechecked2021 Assigned to ICON
41234567890202501149YLocation Fails: Reframe PoleICON: J. WELCH9333699
5F5432102501169Wire Length, Downstream FuseNot found in Overall Checklist2021 Assigned to ICON
69999999999202501189Max KVAYNot found in Overall ChecklistICON: A. KAONO9687788
Old Master


I want to look up value in col A on New and find in col A on Old. If found return col G, I, K, L from Old to col C, D, E, F on New. If not found, lookup up value in col B and return same if found. If not found, return Not Found, move to next value and repeat.

Example of Finished Product
If I went this route, I would Copy and Paste Values to remove formulas, but I think that would slow me down a ton.

Cell Formulas
RangeFormula
C2:C6C2=XLOOKUP($A2,'[VBA Example.xlsx]Old Master'!$A$2:$A$6,'[VBA Example.xlsx]Old Master'!G$2:G$6,XLOOKUP('Finished Example'!$B2,'[VBA Example.xlsx]Old Master'!$A$2:$A$6,'[VBA Example.xlsx]Old Master'!G$2:G$6,"Not found"))&""
D2:D6D2=XLOOKUP($A2,'[VBA Example.xlsx]Old Master'!$A$2:$A$6,'[VBA Example.xlsx]Old Master'!I$2:I$6,XLOOKUP('Finished Example'!$B2,'[VBA Example.xlsx]Old Master'!$A$2:$A$6,'[VBA Example.xlsx]Old Master'!I$2:I$6,"Not found"))&""
E2:F6E2=XLOOKUP($A2,'[VBA Example.xlsx]Old Master'!$A$2:$A$6,'[VBA Example.xlsx]Old Master'!K$2:K$6,XLOOKUP('Finished Example'!$B2,'[VBA Example.xlsx]Old Master'!$A$2:$A$6,'[VBA Example.xlsx]Old Master'!K$2:K$6,"Not found"))&""
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi, here is a simple solution with VBA

Note:
You will have to run it in you new file and change a little bit of code of the the offset, if the columns had changed.
If the CPN# or TLN# is duplicated, it will take the last one.

Excel Formula:
Option Explicit
'Set the variable
'We will assume your VBA codes are in the New Master file

'Where to look up for the data, ASSUMING they are in the same folder
Dim OldWB As Excel.Workbook
Dim OldWS As Excel.Worksheet
Dim OldRange, OldLine As Range 'To set the range to look for
Dim OldLastR As Integer 'use to set the Range to lookup for

Dim LastR, i As Integer


Sub ImportOldData()
'Set the path
Set OldWB = Excel.Workbooks("Old Master.xlsx") 'change the name of the file here
Set OldWS = OldWB.Worksheets("Old Master") 'choose the sheet here. Note, the Excel has changed.
'OldWS.Range("N1").Value = "Sucess" 'To test the "connection" works
OldLastR = OldWS.Cells(Rows.Count, 1).End(xlUp).Row 'Find the last row of the Old Master
Set OldRange = OldWS.Range("A2:A" & OldLastR) 'Set the range to look for the value in the Old Master

LastR = Cells(Rows.Count, 1).End(xlUp).Row 'To find the last items to look for

For i = 2 To LastR
For Each OldLine In OldRange
If OldLine.Value = Cells(i, 1).Value Or OldLine.Value = Cells(i, 2).Value Then
Cells(i, 3).Value = OldLine.Offset(, 6).Value 'Reason, 6 columns from the found data, like a vlookup
Cells(i, 4).Value = OldLine.Offset(, 8).Value 'Precheck, 8 columns from the found data, like a vlookup
Cells(i, 5).Value = OldLine.Offset(, 10).Value 'Name, 10 columns from the found data, like a vlookup
Cells(i, 6).Value = OldLine.Offset(, 11).Value 'WR#, 11 columns from the found data, like a vlookup
End If
Next OldLine
Next i

End Sub

VBA Code:
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top