Index, Match VBA

Micheal_

New Member
Joined
Jun 17, 2015
Messages
3
Dears,

I have the following data from workbook1:


Don't careDon't CareImportantLocationID
xxengine=0,model=14,slot=don't care,card=3,ID=4Germany_1
xxengine=0,model=14,slot=don't care,card=2,ID=4Germany_1
xxengine=0,model=14,slot=don't care,card=2,ID=10Japan_2
xxengine=0,model=14,slot=don't care,card=2,ID=10Germany_2

<tbody>
</tbody>

In worksheet2 (another excel file) , i have the following format:

LocationIDenginemodelCardIDNameAlias
Germany_101434y66666
Germany_101424x7777
Japan_2014210z8888
Germany_2014210u9999

<tbody>
</tbody>

What i need, is a macro that match the values for each of "engine,model,card,ID" & LocationID which are available under "Important" and "LocationID" columns from workbook 1 with their respective values in Workbook 2 to get the "Alias".












<tbody>
</tbody>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'm not sure if you have two distinct workbooks, or two distinct worksheets based upon what you wrote.

If they are two worksheets in a single book, you can use the following code:

Code:
Sub FindAlias()


i = 2
Do Until Sheet1.Cells(i, 3) = ""
    ImportantInfo = Split(Sheet1.Cells(i, 3), ",")
    
    Engine = Split(ImportantInfo(0), "=")
    Engine = Engine(1)
    Model = Split(ImportantInfo(1), "=")
    Model = Model(1)
     card = Split(ImportantInfo(3), "=")
    card = card(1)
    TheID = Split(ImportantInfo(4), "=")
    TheID = TheID(1)
    CountryName = Sheet1.Cells(i, 4)
    
    j = 2
    Foundit = 0
    Do Until Sheet2.Cells(j, 1) = "" Or Foundit = 1
        If Sheet2.Cells(j, 1) = CountryName _
            And Sheet2.Cells(j, 2) = Engine _
            And Sheet2.Cells(j, 3) = Model _
            And Sheet2.Cells(j, 4) = card _
            And Sheet2.Cells(j, 5) = TheID Then
                Foundit = 1
                Sheet1.Cells(i, 5) = Sheet2.Cells(j, 7)
        End If
        j = j + 1
    Loop
    
    i = i + 1
Loop



End Sub

if you have two distinct workbooks, you will need to modify the code for to account for the qualifying of the workbooks.
 
Upvote 0
I'm not sure if you have two distinct workbooks, or two distinct worksheets based upon what you wrote.

If they are two worksheets in a single book, you can use the following code:

Code:
Sub FindAlias()


i = 2
Do Until Sheet1.Cells(i, 3) = ""
    ImportantInfo = Split(Sheet1.Cells(i, 3), ",")
    
    Engine = Split(ImportantInfo(0), "=")
    Engine = Engine(1)
    Model = Split(ImportantInfo(1), "=")
    Model = Model(1)
     card = Split(ImportantInfo(3), "=")
    card = card(1)
    TheID = Split(ImportantInfo(4), "=")
    TheID = TheID(1)
    CountryName = Sheet1.Cells(i, 4)
    
    j = 2
    Foundit = 0
    Do Until Sheet2.Cells(j, 1) = "" Or Foundit = 1
        If Sheet2.Cells(j, 1) = CountryName _
            And Sheet2.Cells(j, 2) = Engine _
            And Sheet2.Cells(j, 3) = Model _
            And Sheet2.Cells(j, 4) = card _
            And Sheet2.Cells(j, 5) = TheID Then
                Foundit = 1
                Sheet1.Cells(i, 5) = Sheet2.Cells(j, 7)
        End If
        j = j + 1
    Loop
    
    i = i + 1
Loop



End Sub

if you have two distinct workbooks, you will need to modify the code for to account for the qualifying of the workbooks.


Dear Puertorekinsam,

Thank you for your feedback.


I've executed the above VBA but did not get any result even no error in the debug

Moreover,Please to help on the below:

If I need to add amend the above VBA code as per the following new rules:

1-I want to execute the above VBA code only if the string value available under column C is equal to a defined statement ( e.g.: the statement that needs to be verified to execute the code is equal to "File is OK" )

2- The header names in sheet1 may differ than those available in sheet 2 & might contains space
e.g. In sheet1 , we have "Country ID" ; whereas in sheet 2 it is named "Location ID"

Thank you
 
Upvote 0
The code I wrote does not look at column headers only position. It assumes that Important is in column C and Location ID is in Column D on Sheet 1

Likewise, it assumes that the second set of data is in Columns A through G on Sheet2.

I know you said they were in separate workbooks, we need some additional information, such as what are the workbooks named? How do we know what is workbook1 and 2 (name or qualifying cell to look for)
 
Upvote 0

Forum statistics

Threads
1,213,568
Messages
6,114,348
Members
448,570
Latest member
rik81h

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