Excel/Macro for lookup

Advertisement


Question
Macro Requirement
Macro Requirement  
Hi Tom,

I need a macro for the below data. I have given the tables below and the required output.

Data 1          Data 2          Output
LANE_A   |OD1       LANE_A | S1          OD1  | S1
LANE_A   |OD2       LANE_A | S2          OD1  | S2
LANE_B   |OD3       LANE_A | S3          OD1  | S3
LANE_B   |OD4       LANE_B | S4          OD2  | S1
         LANE_B | S5          OD2  | S2
         OD2  | S3
         OD3  | S4
         OD3  | S5
         OD4  | S4
         OD4  | S5
         
PS: Data 1 , Data 2 and the output has 2 columns has 2 colums

Answer
sudharshan,
You didn't give locations in terms of what cells, so I overlayed your picture starting in A1


so for Data 1, the first Lane_A  was in cell A2
for Data 2, the first Lane_A was in D2

I started my output in G2

change the locations in the code to reflect your locations.  My code assumes that Data 1 and Data 2 is the only data on the sheet (there is no data below data 1 and data 2 - this assumption is associated with how the code finds the bottom of the data.


Sub combinedata()
Dim Data1 As Range, Data2 As Range
Dim Results As Range, cell1 As Range, cell2 As Range
Dim i As Long
Set Data1 = Range("A2", Cells(Rows.Count, "A").End(xlUp))
Set Data2 = Range("D2", Cells(Rows.Count, "D").End(xlUp))
Set Results = Range("G2")
i = 0
For Each cell1 In Data1
 For Each cell2 In Data2
   If cell1 = cell2 Then
      Results.Offset(i, 0).Value = cell1.Offset(0, 1)
      Results.Offset(i, 1).Value = cell2.Offset(0, 1)
      i = i + 1
   End If
 Next
Next
      
End Sub


Produced the output you show.

Test it on a copy of yow worksheet.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.