简体   繁体   English

查找一列中具有相同值的单元格,并从同一行的不同列中返回值

[英]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. 假设我在单元格L13L15L20找到了值,我想将单元格D13D15D20的值返回到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,你会想改变wrkinRngoutRng变量被命名区域您定义和更改列索引forif块匹配您正在寻找的数据的列索引。

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.

相关问题 在列中查找值并选择同一行中的单元格 - find values in a column and select the cells in the same row 在列中查找值并返回同一行但不同列的值 - Find a value in a column and return value of same row but different column 查找列中的值,然后更改同一行中其他位置的值 - Find a value within a column, then change a value elsewhere on the same row 如何在列中查找字符串值并返回该行中所有单元格中的值? - How to find a string value in a column and return values in all cells from that row? 批量查找并替换列单元格中的相同值 - Bulk find and replace same values of in cells of a column 在一个表中查找一个值,将数据复制到同一行的另一列中,然后粘贴到另一张表中 - Find a value in one table, copying data in a different column from same row and then paste into another table 搜索一列字符串并将值添加到同一行的四个不同单元格 - Search one column for string and add values to four different cells in same row 如果某些列值相同,如何在VBA中合并一行单元格 - How to combine a row of cells in VBA if certain column values are the same 我需要根据一行中单元格的值从一个表返回多个列标题 - I need to return multiple column headers from a table based on the value of the cells within a row 访问 Excel VBA 在列下查找具有相同值的单元格 - Access Excel VBA find cells with same value under a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM