简体   繁体   中英

How to search 2 values on another Sheet and then based on that return a specific cell from the same row as them back on the 1st Sheet?

I was thinking of a double VBA VLOOKUP or a double conditional If for that 2 cells and return another cell from the specific row back on Sheet1.

Basically a double VBA VLOOKUP or a double conditional IF when the strings were found I'd be going with the VLOOKUP in VBA , sort of look for A2 and D2 from Sheet 1 in Sheet 2 Range A1:A999 and D2:D999 and if the results match output C cell from the same row back on C2 in Sheet 1

But not sure how to proceed further. Any advice would be more than appreciated. Thanks!

黄色单元格上的输出

将发生 VBA VLOOKUP 的 Sheet2

The following vba macro might be of help:

Sub lookupMatch()
    Dim TotalCols As Long
    Dim rng As Range
    Dim i As Long

    'Copy matched up values from sheet2 to sheet1
    Sheets("Sheet2").Select
    TotalCols = ActiveSheet.UsedRange.Columns.Count

    'Changed Cols to Columns on the above line.

    Range("C1:C" & TotalCols).Copy Destination:=Sheets("Sheet1").Range("C2")

    'destination sheet
    Sheets("Sheet2").Select

    For i = 1 To TotalCols
        'Search for the value on sheet2
        Set rng = Sheets("Sheet2").UsedRange.Find(Cells(i, 3).Value)
        'If found put its value on destination sheet
        If Not rng Is Nothing Then
            Cells(i, 2).Value = rng.Value
        End If
    Next
End Sub

Doable entirely without VBA:

=INDEX(Sheet2!$C$1:$C$999, AGGREGATE(15, 6, ROW(Sheet2!$A$1:$A$999)/(--(Sheet2!$A$1:$A$999=$A1)*--(Sheet2!$B$1:$B$999=$B1)), 1), 1)

The obvious bit is the Index - replace the AGGREGATE with a MATCH , and you probably already recognise it:

=INDEX(Sheet2!$C$1:$C$999, MATCH(???), 1)

However, instead of using MATCH , we are using the AGGREGATE function

AGGREGATE(15, 6, ROW(Sheet2!$A$1:$A$999)/(--(Sheet2!$A$1:$A$999=$A1)*--(Sheet2!$B$1:$B$999=$B1)), 1)
  1. The first argument ( 15 ) just tells us to look for the k th SMALL est value in a list. This is similar to using the SMALL function
  2. The second argument ( 6 ) tells us to discard any error values in the list.
  3. The third argument is the list to look at - we'll break this down later
  4. The final argument ( 1 ) tells us to get the 1 st smallest value from the list - effectively MIN , but MIN doesn't play nicely with calculated lists

So, what is our List then. Let's look at the formula:

ROW(Sheet2!$A$1:$A$999)/(--(Sheet2!$A$1:$A$999=$A1)*--(Sheet2!$B$1:$B$999=$B1))

There are two important parts here - first, ROW(Sheet2!$A$1:$A$999) will output the row number that we want, so that we can use it in the INDEX . Secondly, we divide it by a value which will be 1 when Column A matches ( --(Sheet2!$A$1:$A$999=$A1) ) and Column B matches ( --(Sheet2!$B$1:$B$999=$B1) ), but 0 when either of them doesn't. And, anything divided by 0 gives a #DIV0! error - which means it is discarded from our list!

So, the output of the AGGREGATE is the smallest Row number where the values match. If there is no such row , then you will get a #NUM! error instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM