问题
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!
回答1:
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)
- The first argument (
15) just tells us to look for the kth SMALLest value in a list. This is similar to using theSMALLfunction - The second argument (
6) tells us to discard any error values in the list. - The third argument is the list to look at - we'll break this down later
- The final argument (
1) tells us to get the 1st smallest value from the list - effectivelyMIN, butMINdoesn'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.
回答2:
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
来源:https://stackoverflow.com/questions/59984696/how-to-search-2-values-on-another-sheet-and-then-based-on-that-return-a-specific