A Christmas gift exchange needs two constraints at the same time: nobody can receive the same person as last year, and nobody can receive themselves. A direct random shuffle can satisfy one rule while breaking the other, especially when only a few names remain. A rotating list gives a simpler and more reliable solution for small-business teams and family groups.
The problem
The workbook has three columns:
- Name list: the person giving a present this year.
- Previous name: the person that giver selected last year.
- Name to use: the new recipient.
For each row, the new recipient must differ from both the giver and the previous recipient. For example, Albert cannot receive Albert’s present, and Albert also cannot receive the person he had last year.
This came up in the Stack Overflow community, where the suggested solution was to use a list rotation instead of repeatedly trying random swaps. The original problem and answer are available in the Stack Overflow discussion about avoiding the same position and previous assignment.
The key technical point is that this is a constrained shuffle. In mathematics, a rearrangement where no item stays in its original position is called a derangement. Adding the previous assignment creates another restriction. Some combinations have no valid answer at all, so a formula that chooses names one row at a time can eventually get stuck.
The solution
Use the previous assignments as the starting list, then shift that list by one position. This creates a new assignment without repeating the previous one, provided the original exchange was valid.
Suppose the previous assignments are in B2:B6. Put this formula in C2:
=INDEX($B$2:$B$6,MOD(ROW()-ROW($C$2)+1,ROWS($B$2:$B$6))+1)
The formula shifts every previous name down by one position:
- The first row receives the second name from the previous list.
- The second row receives the third name.
- The final row receives the first name.
INDEX returns a name from the range. ROW() identifies the current worksheet row. MOD wraps the position back to the beginning when the formula reaches the end. ROWS counts the number of names in the list.
For five people, the result looks like this:
| Name list | Previous name | Name to use |
|---|---|---|
| Albert | Bob | Elmer |
| Bob | Elmer | Chad |
| Chad | David | Albert |
| David | Chad | Bob |
| Elmer | Albert | David |
The exact result depends on the order of the previous-name column. The important property is that each row receives a different entry from that column, and the shifted arrangement can be checked against the name list.
A more robust annual process is to maintain a single assignment list:
- Create a valid first-year list by shuffling the names.
- Shift that list by one position for the next year.
- Shift the new list again the following year.
- Continue rotating the list once per year.
This method avoids the need for a VBA Exclude function. It also avoids the risk of choosing a valid-looking name early in the process and leaving no valid choice for the final rows.
The alternative is a formula using LET, LAMBDA, REDUCE, FILTER, XMATCH, VSTACK, and TAKE. That approach can select the first available name while excluding the giver, the previous name, and names already assigned. It is more flexible, but a random version can return blank results when the remaining constraints cannot be satisfied. Use it only when the assignment rules require more than a simple rotation.
How to apply it
- Put each giver’s name in column A, starting in
A2. - Put last year’s recipient for each giver in column B, starting in
B2. - Confirm that every name appears once in column A and once in column B.
- Add the formula below to
C2:
=INDEX($B$2:$B$6,MOD(ROW()-ROW($C$2)+1,ROWS($B$2:$B$6))+1)
- Replace
$B$2:$B$6with the complete previous-name range. - Copy the formula down to the final participant.
- Check that column C contains no duplicate names.
- Check that each value in column C differs from both columns A and B on the same row.
- For the next year, copy column C and paste it as values into column B.
- Apply the same formula again to create the next year’s assignment.
The formula above is deterministic, not random. That is useful when reliability matters more than unpredictability. If the gift exchange needs a random first-year arrangement, shuffle the initial list once, then use the rotation method in later years.
For larger groups, do not assume that a valid result always exists. Two participants can create an impossible case if each person is blocked from receiving the only remaining name. This is the edge case that makes a simple random algorithm unreliable. A VBA procedure can search for swaps, but the logic becomes more complicated near the penultimate element, the second-to-last item, and the last item. It may need a quadratic algorithm, meaning it repeatedly searches earlier assignments, rather than a linear algorithm that processes each row once.
Keep the source list clean. Names should match exactly, with no extra spaces or spelling differences. If the workbook contains duplicate names, use a unique employee or family identifier in the calculation and display the person’s name separately.
Implementation angle
This pattern can become a reusable annual assignment workbook with protected input columns, automatic validation, and an exception report for impossible combinations. An automation partner could move the participant list into Zoho Creator, generate the rotated assignments, and send each recipient a private email or message through Make or n8n. Claude can also help build and check the formula, but the final workbook should validate duplicates, self-assignments, and repeated previous names before anyone receives a result.
Book an AI implementation engagement with Creator Scripts at creatorscripts.com to turn this assignment process into a reliable workflow.
Frequently asked questions
How do I assign gift exchange names in Excel without repeating last year?
Put last year’s recipients in a column and use the INDEX and MOD formula to rotate that list by one position. Copy the formula down, then check that no one is assigned to themselves or their previous recipient.
What Excel formula shifts a list of names by one row?
Use =INDEX($B$2:$B$6,MOD(ROW()-ROW($C$2)+1,ROWS($B$2:$B$6))+1) in C2, changing the ranges to match your workbook. Copy it down to rotate the names, wrapping the last entry back to the first.
Can every gift exchange group avoid self-assignments and repeat recipients?
No, some combinations of participants and restrictions have no valid assignment. Check that the final list has no duplicate recipients and that each recipient differs from both the giver and last year’s recipient.