How to edit this return multiple matches VBA code to input matches below?

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have this VBA code that I found on this site. I have tweaked it a tiny bit for my needs. It essentially is a vlookup but returns multiple matches. However, it inputs the matches next to the desWs value (“A1”). Goes column by columns. How can I edit this to input the data below? Also I would like to be able edit the return matches location as the lookup value will be in “A1” but I would like some matches to go in column A and also column B. This will be done by running this code multiple times with different column numbers to search for on Sheet1.

I have tried editing editing the code a number of times with no luck. Either it doesn’t input the data down bottom or it doesn’t return all matches. Currently I use an array formula to do this but it slows down my file heavily. What I am looking for in my final result is shown in the attached picture. And here is the code. Thank you all.

VBA Code:
Sub ReturnMultipleMatches()



Application.ScreenUpdating = False

Dim LastRow1 As Long, LastRow3 As Long, rng As Range, sAddr As String, Val As Range, lCol As Long, desWS As Worksheet, srcWS As Worksheet

Set desWS = Sheets("Sheet3")

Set scrWS = Sheets("Sheet1")



LastRow1 = scrWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

LastRow3 = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row



For Each rng In desWS.Range("A1")

Set Val = scrWS.Range("A2:AH" & LastRow1).Find(rng, LookIn:=xlValues, lookat:=xlWhole)

If Not Val Is Nothing Then

sAddr = Val.Address

Do

lCol = desWS.Cells(rng.Row, desWS.Columns.Count).End(xlToLeft).Column + 1

desWS.Cells(rng.Row, lCol) = scrWS.Cells(Val.Row, 30)

Set Val = scrWS.Range("A2:AH" & LastRow1).FindNext(Val)

Loop While Val.Address <> sAddr

sAddr = ""

End If

Next rng

Application.ScreenUpdating = True



End Sub
 

Attachments

  • 32C231AF-3C81-4D04-95B4-9F35FF665DC0.jpeg
    32C231AF-3C81-4D04-95B4-9F35FF665DC0.jpeg
    205.1 KB · Views: 36

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,903
Messages
6,181,658
Members
453,059
Latest member
jkevin

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