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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,223,948
Messages
6,175,579
Members
452,653
Latest member
craigje92

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