Home » excel » How to Auto-Fill a Reverse Pair Combination in Excel

# How to Auto-Fill a Reverse Pair Combination in Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a spreadsheet that looks like this: I am currently manually inputting my ‘Y’ and ‘N’ values into my Value column based on research. However, to save time, I would like the value for the initial pair to be auto-filled for its reverse pairing. For example, I gave the apple to orange pairing a value of ‘Y’. I would like to have auto-filled the orange to apple pairing with the same ‘Y’ value. I thought of doing index match, but it doesn’t take into consideration the combinations of pairings. Any thoughts would be appreciated.

How to&Answers:

I don’t know about a formula, but here’s a VBA solution – you could even turn this into a `Worksheet_Change` event which would constantly refresh values if you made an changes to the column itself.

As Marc said – you shouldn’t really have a column with a mix of values and formulas (especially if it’s reading off of itself, then you’re going to run into a problem of recursion).

``````Option Explicit
Sub PopulateValue()

Dim i As Long, j As Long

For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row

If Cells(i, 3).Value = "" Then

'Look for a true match
For j = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If i <> j Then
If Cells(i, 1).Value = Cells(j, 1).Value And _
Cells(i, 2).Value = Cells(j, 2).Value Then
If Cells(j, 3).Value <> "" Then
Cells(i, 3).Value = Cells(j, 3).Value
Exit For
End If
End If
End If
Next j

'Look for a reverse match
For j = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If i <> j Then
If Cells(i, 1).Value = Cells(j, 2).Value And _
Cells(i, 2).Value = Cells(j, 2).Value Then
If Cells(j, 3).Value <> "" Then
Cells(i, 3).Value = Cells(j, 3).Value
Exit For
End If
End If
End If
Next j
End If
Next i

End Sub
`````` ### Answer：

Here’s a worksheet formula. Assuming Header in row 1, data starting in row 2, and `Entity1`, `Entity2` and `Value` in columns `A`, `B` and `C`, respectively, put

``````=IFERROR(INDEX(C\$2:C2,MATCH(B3&"\$"&A3,A\$2:A2&"\$"&B\$2:B2,0)),"")
``````

in `C3` as an array formula (`CTRL-SHIFT-ENTER`) and fill down.

Explanation

The formula does an index/match like you considered. For the lookup part of the match it concatenates `Entity2` and `Entity` into a single string. There is an optional `\$` delimiter between two two entities (explained below). For the array part of the match it `Entity1` and `Entity2` joined by the same delimiter. The array starts in the first data row and ends in the row above the current row.

If the match returns an index, that is plugged into `INDEX` along with the array of values up to the present one. If the match returns a #VALUE error, it gets caught by wrapping in an `IFERROR` function.

Delimiter Consider two pairs of `Entity1` and `Entity2`, the first pair being (pikes, peak) and the second being (pike, speak). Concatenating either of these without a delimiter gives “pikespeak” and could lead to an erroneous match. The delimiter guards against that. You can choose any string that down not occur in your data as a delimiter.

Hope that helps