Function Problems

Return a Source Column Value When Matching Excel Tables with VBA

Huma Ashraf ChHuma Ashraf Ch Nov 18, 2026 869 views

Question details

Match customer and vehicle records from two Excel tables, copy the results to a separate sheet, and return a value from source column A.

Return a Source Column Value When Matching Excel Tables with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user wants to combine customer and inventory records using shared vehicle attributes and include a source-column value in the output.
Observed behavior
The desired result is a separate table containing customer, inventory, car, color, and interior information for matching records.
Before you start

Confirm which fields define a match, which table contains the column A value, and how duplicate matches should be handled. The examples below assume Customers has Customer, Car, Color, and Interior in columns A–D, while Inventory has an inventory value in column A and Car, Color, and Interior in columns B–D, with headers in row 1.

Solution 1Recommended

Match the tables and return inventory column A with VBA

Use this macro to create a separate results sheet and return the first inventory match for each customer.

The macro compares Car, Color, and Interior without regard to case and ignores leading and trailing spaces. It writes one row per customer, marks unmatched records as No match, and returns only the first match when inventory records are duplicated.

The example expects worksheets named Customers and Inventory in the workbook containing the macro. It stops if a sheet named MatchResults already exists.

1
Prepare the workbook

Check the Customers and Inventory sheet names and column layouts described above. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) using File > Save As.

2
Insert the macro

Open Developer > Visual Basic, select the workbook, and choose Insert > Module. Paste this code into the module: Sub MatchCustomerInventory() Dim wsC As Worksheet, wsI As Worksheet, wsR As Worksheet Dim lastC As Long, lastI As Long, r As Long, i As Long Dim found As Boolean Set wsC = ThisWorkbook.Worksheets("Customers") Set wsI = ThisWorkbook.Worksheets("Inventory") On Error Resume Next Set wsR = ThisWorkbook.Worksheets("MatchResults") On Error GoTo 0 If Not wsR Is Nothing Then MsgBox "A MatchResults sheet already exists. Rename or remove it before running the macro." Exit Sub End If Set wsR = ThisWorkbook.Worksheets.Add(After:=wsI) wsR.Name = "MatchResults" wsR.Range("A1:E1").Value = Array("Customer", "Inventory", "Car", "Color", "Interior") lastC = wsC.Cells(wsC.Rows.Count, "A").End(xlUp).Row lastI = wsI.Cells(wsI.Rows.Count, "A").End(xlUp).Row For r = 2 To lastC wsR.Cells(r, 1).Value = wsC.Cells(r, 1).Value wsR.Cells(r, 3).Value = wsC.Cells(r, 2).Value wsR.Cells(r, 4).Value = wsC.Cells(r, 3).Value wsR.Cells(r, 5).Value = wsC.Cells(r, 4).Value found = False For i = 2 To lastI If StrComp(Trim(CStr(wsC.Cells(r, 2).Value)), Trim(CStr(wsI.Cells(i, 2).Value)), vbTextCompare) = 0 And _ StrComp(Trim(CStr(wsC.Cells(r, 3).Value)), Trim(CStr(wsI.Cells(i, 3).Value)), vbTextCompare) = 0 And _ StrComp(Trim(CStr(wsC.Cells(r, 4).Value)), Trim(CStr(wsI.Cells(i, 4).Value)), vbTextCompare) = 0 Then wsR.Cells(r, 2).Value = wsI.Cells(i, 1).Value found = True Exit For End If Next i If Not found Then wsR.Cells(r, 2).Value = "No match" Next r End Sub

3
Run and review the results

In Excel, choose Developer > Macros, select MatchCustomerInventory, and click Run. Open MatchResults and check the returned inventory values and any rows marked No match.

Match the tables and return inventory column A with VBA
Adjust the match if needed: The macro assumes column A is populated for each source record and that the matching cells contain ordinary text or values, not errors. Change the column references and comparison rules if your actual tables use different fields or require all duplicate matches.
Match records without VBA

Match customer and inventory records in WPS Spreadsheets

Use a helper column with INDEX and MATCH in WPS Spreadsheets to return inventory values and build a separate results sheet without writing a macro.

  1. 1. Open and check the workbook: Open the workbook in WPS Spreadsheets. Confirm that Customers and Inventory use the column layouts described above and that Inventory column E is empty.
  2. 2. Add a match key: In Inventory!E2, enter =B2&"|"&C2&"|"&D2 and fill it down. Use this key only if the vehicle fields do not contain the | character and have consistent spacing.
  3. 3. Arrange the output: Add a Results sheet and label A1:E1 Customer, Inventory, Car, Color, and Interior. Copy the customer data into columns A, C, D, and E.
  4. 4. Look up the inventory value: In Results!B2, enter =IFERROR(INDEX(Inventory!$A$2:$A$1000,MATCH(C2&"|"&D2&"|"&E2,Inventory!$E$2:$E$1000,0)),"No match") and fill down. Extend the ranges if needed, then check unmatched rows.
Match vehicle attributes and return a value from inventory column AOpen and edit Microsoft Excel XLSX workbooksBuild a separate results sheet with familiar spreadsheet formulas
microsoft office alternative - wps office

Frequently Asked Questions

Which fields does the example use to match records?

It matches Car, Color, and Interior in columns B–D of both Customers and Inventory. If your matching key is different, change the VBA comparisons or the helper-key formula accordingly.

What happens if more than one inventory row matches?

Both examples return the column A value from the first matching inventory row. To return every match, use a different macro or a Power Query merge designed for multiple matches.

Why does a customer show No match?

Check the Car, Color, and Interior values on both sheets, including spelling and spacing. The VBA example trims leading and trailing spaces; the formula example requires consistent spacing.

Can I use Power Query instead of VBA?

Yes. In Excel, load the two ranges through Data > From Table/Range, then use Merge Queries and select the matching columns in the same order. Expand the merged Inventory table to include its column A value and load the result to a worksheet.