VBA lookup by taking common column from user

sanju1988

New Member
Joined
Jun 12, 2022
Messages
10
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
Sub VlookupDataAcrossMultipleTabs()
Dim mainFile As Workbook
Dim lookupFile As Workbook
Dim mainSheet As Worksheet
Dim lookupSheet As Worksheet
Dim mainFilePath As String
Dim lookupFilePath As String
Dim mainCol As String
Dim lookupCol As String
Dim lookupCols As String
Dim lookupColsArray() As String
Dim lastRowMain As Long
Dim lastRowLookup As Long
Dim cell As Range
Dim i As Long
Dim colNum As Long
Dim lookupValue As Variant
Dim saveFolderPath As String
Dim newFileName As String

' Select the main file
mainFilePath = Application.GetOpenFilename("Excel Files (*.xls; *.xlsx), *.xls; *.xlsx", , "Select Main File")
If mainFilePath = "False" Then Exit Sub
Set mainFile = Workbooks.Open(mainFilePath)

' Select the lookup file
lookupFilePath = Application.GetOpenFilename("Excel Files (*.xls; *.xlsx), *.xls; *.xlsx", , "Select Lookup File")
If lookupFilePath = "False" Then Exit Sub
Set lookupFile = Workbooks.Open(lookupFilePath)

' Select the save folder
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the folder to save the updated main file"
If .Show = -1 Then
saveFolderPath = .SelectedItems(1)
Else
Exit Sub
End If
End With

' Loop through each sheet in the main file
For Each mainSheet In mainFile.Worksheets
' Get the common column in main file
mainCol = InputBox("Enter the common column name in main file (Sheet: " & mainSheet.Name & ")")

' Loop through each sheet in the lookup file
For Each lookupSheet In lookupFile.Worksheets
' Get the common column in lookup file
lookupCol = InputBox("Enter the common column name in lookup file (Sheet: " & lookupSheet.Name & ")")

' Get the columns to lookup from the lookup file
lookupCols = InputBox("Enter the columns to lookup from lookup file (comma separated, Sheet: " & lookupSheet.Name & ")")
lookupColsArray = Split(lookupCols, ",")

' Find the last row in both sheets
lastRowMain = mainSheet.Cells(mainSheet.Rows.Count, mainCol).End(xlUp).Row
lastRowLookup = lookupSheet.Cells(lookupSheet.Rows.Count, lookupCol).End(xlUp).Row

' Perform the lookup
For Each cell In mainSheet.Range(mainCol & "2:" & mainCol & lastRowMain)
lookupValue = Application.Match(cell.Value, lookupSheet.Range(lookupCol & "1:" & lookupCol & lastRowLookup), 0)

If Not IsError(lookupValue) Then
For i = LBound(lookupColsArray) To UBound(lookupColsArray)
colNum = Application.Match(Trim(lookupColsArray(i)), lookupSheet.Rows(1), 0)
If Not IsError(colNum) Then
cell.Offset(0, i + 1).Value = lookupSheet.Cells(lookupValue, colNum).Value
End If
Next i
End If
Next cell
Next lookupSheet
Next mainSheet

' Ask for the new file name and save the main file
newFileName = InputBox("Enter the new file name (without extension):")
mainFile.SaveAs Filename:=saveFolderPath & "\" & newFileName & ".xlsx"
mainFile.Close SaveChanges:=True
lookupFile.Close SaveChanges:=False

MsgBox "VLOOKUP operation completed successfully. File saved as " & saveFolderPath & "\" & newFileName & ".xlsx", vbInformation
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
.Trying to vlookup by selecting common column and lookup columns but giving error
 
Upvote 0
It would help if you told us which line of code produces the error, and what error message you're getting.

But I'm guessing you are getting a Type mismatch error on the first line below, if a MATCH fails, because you have declared colNum as Long, rather than Variant?

VBA Code:
colNum = Application.Match(Trim(lookupColsArray(i)), lookupSheet.Rows(1), 0)
If Not IsError(colNum) Then
    '.....
 
Upvote 0

Forum statistics

Threads
1,222,123
Messages
6,164,090
Members
451,872
Latest member
TimothyLynn

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