VBA macro to match cells according to user selection

jabutu

New Member
Joined
Mar 6, 2014
Messages
2
Hi all,

I'm new to the forum and hoping somebody can help me. I've been trying to code the following functions into a macro but can't figure out how to achieve it. I'm stuck on step 3 and would really appreciate some help with this:

Functions:
1) Match the cell values in Worksheet 1, column A, with the values in Worksheet 2, Column A
2) Output the resultant matched rows from Worksheet 1 and 2 into Worksheet 3
....and here's the bit I can't get.....
3) Where there is more than one match in worksheet 2, ask the user to choose which match to output into Worksheet 3

Since that is mighty confusing, here's a simplified dataset to illustrate :)
Note that I can't change the format of Worksheet 2, which has the "Surname" also shown in the "Forename" column (in bold).

Worksheet 1: Values to be searched for (Col A)
A B
1Forename Age
2Bob22
3Jannet56
4Julie43
5John23
6Micheal11
7Jeff23

<colgroup><col span="2"><col><col></colgroup><tbody>
</tbody>

Worksheet 2: Data in which to find a match (col A)
ABC
1ForenameSurnameShirt Colour
2Rands
3BobRandsBlue
4JannetRandsWhite
5JulieRandsYellow
6JohnRandsMaroon
7Jamison
8MichealJamisonRed
9JeffJamisonOrange
10BobJamisonYellow
11SarahJamisonBlack
12SimonJamisonGreen
13ClarkJamisonPurple
14Teller
15ThomasTellerWhite
16CandyTellerGrey
17SarahTellerCrimson
18JaneTellerAqua
19AnetteTellerYellow

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>

Worksheet 3: Output data from Worksheets 1 and 2
A B CD
1Forename Age SurnameShirt Colour
2Bob22 JamisonYellow
3Jannet56 RandsWhite
4Julie43 RandsYellow
5John23 RandsMaroon
6Micheal11 JamisonRed
7Jeff23 JamisonOrange

<colgroup><col span="3"><col><col><col></colgroup><tbody>
</tbody>

Notice that the first entry, "Bob", would not be the first match in Worksheet 2 as it is the wrong Surname. This is where user input would be required and I'd ideally like excel to ask the user to select which match the data should be taken from. Ideally this would be a dropdown list or similar.

Thanks a lot for any help you can give!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Well I figured out a way. For anyone else in a similar situation, or anyone just interested, this is how I did it using two separate macros:

Macro 1:
1) Searched Worksheet 2 for a match to the row selected on Worksheet 1 (e.g. Bob)
2) For each match found within worksheet 2, populate a row of cells within Worksheet 1 with the relevant data (First name and Surname here) from Worksheet 2, including the row number of the matched data (e.g. Row 3 = Bob Rands, and Row 10 = Bob Jamison).
3) For a cell within Worksheet 1 and within the currently selected row (row 2 of worksheet 1 in this example), create a cell with list validation and list range equal to the matches recorded in step 2.
End of Macro 1

User input stage:
User selects the correct match in the drop down list created in step 3 above. e.g. Bob Jamison instead of Bob Rands.
End of user input.

Macro 2:
Using the selections defined by the user, with reference to the matched row, populate worksheet 3 with all the required data.
End of Macro 2.

Add in some other efficiency boosting options, like highlighting cells with more than one match, and this has worked very efficiently for me.

Hope this helps someone else out there!
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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