I want to find all the cells in Column L
with a particular value and return the values in Column D
of the same row as those cells found.
So far, I am only able to return one result, which would be the top most result in my list, but I want to find all the rest as well, which I don't know the code to use.
Just to further explain: Value in cell D11
is the value I want to find in Column L of sheet "Master List". Supposedly I find the value in cells L13
, L15
and L20
, I want to return the value in cell D13
, D15
and D20
into cells "C37:C39
" of ws. Note: no. of cells that have the value may vary so the values returned will just appear from C37
downwards (something like automatic multiple selection, copy and paste)
Here's a little something to start the ball rolling:
Sub FindRelatedProducts()
Dim cell As Excel.Range
Dim D11Value As Variant
Dim D11Row As Variant
Dim ws As Worksheet: Set ws = Sheets("RShip")
Set cell = ws.Range("D11")
D11Value = cell.Value
With Sheets("Master List")
D11Row = Application.Match(D11Value, .Range("L:L"), 0)
If Not IsError(D11Row) Then
ws.Range("C37") = .Range("D" & D11Row).Value
End If
End With
End Sub
Here's an example using range variables.
You'll want to define a range for the input data range and a range for the output data. Then in the VBA you will want to change the
wrk
,inRng
andoutRng
variables to be the named ranges you defined and change the column indexes in thefor
andif
blocks to match the column index of the data you are looking for.