[英]Find cells with same value within one column and return values from separate column of same row
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. 我想找到具有特定值的Column L
所有单元格,并返回与找到的那些单元格相同的行的Column D
的值。
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". 为了进一步说明:单元格D11
中的值是我想在“主列表”工作表的L列中找到的值。 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. 假设我在单元格L13
, L15
和L20
找到了值,我想将单元格D13
, D15
和D20
的值返回到ws的单元格“ C37:C39
”中。 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) 的具有该值的单元格可能会有所不同,因此返回的值只会从C37
向下显示(类似于自动多项选择,复制和粘贴)
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
and outRng
variables to be the named ranges you defined and change the column indexes in the for
and if
blocks to match the column index of the data you are looking for. 然后,在VBA,你会想改变wrk
, inRng
和outRng
变量被命名区域您定义和更改列索引for
和if
块匹配您正在寻找的数据的列索引。
Option Explicit
Option Base 1
Sub FindValues()
Dim wrk As Worksheet
Dim inRng As Range
Dim outRng As Range
Dim cntr As Long
Dim outCntr As Long
Dim findVal As Double
Set wrk = Worksheets("Data")
Set inRng = wrk.Range("LookupRange")
Set outRng = wrk.Range("OutputRange")
' Clear the output range in case you have fewer values on this run than on the previous one
outRng.ClearContents
' Set the value you are looking for
findVal = 1
' Iterate through the rows in the input range. If you find the result you want then write it to the output range
For cntr = 1 To inRng.Rows.Count
If inRng(cntr, 1) = findVal Then ' Assumes the value you are finding is in column 1 of the input range
outRng(outCntr, 1) = inRng(cntr, 2) ' Assumes the values you are exporting is in column 2 of the input range
outCntr = outCntr + 1
End If
Next cntr
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.