Tuesday, June 28, 2022

Excel Match Pairs

Match Pairs is a fun game and great exercise for your brain. In this post we learn to create the game in Excel using VBA macros. As an example, we will match pairs of teams in the Spanish football premier league La Liga. The macros create a random configuration of teams, cover the cells of the game board, and let users pick cells to show the hidden team. The goal of the game is to match 12 pairs of teams in the shortest time possible and with the lowest number of moves. We are going to see how to create a simplified version with numbers first, and the full game with pictures of the teams next. You can also play Match Pairs La Liga online here.


 Match Pairs of Numbers (simplified version)

The board of the simplified version of the match pairs game is a range of 4x4 cells. The first macro in the Layout module (AddCovers) formats that range adding the borders and interior color to resemble a board with covered cells.


The other macro (ArrangeCards) creates the random configuration of numbers in a separate (and hidden) worksheet. That’s achieved with two loops: the first one to loop through each cell and the second to get random numbers until a number meeting the condition can be added to that cell. That condition is met when the number has not yet been added twice.


We need an event procedure to trigger the main macro (ShowCard) when the player selects a cell within the game board. That’s achieved with the SelectionChange event and its Target object variable as follows:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     If Not Application.Intersect(Range("B4:E7"), Target) Is Nothing Then
         ShowCard
     End If
 End Sub

 

The macro ‘ShowCard’ reveals the number for the selected cell by setting the background color to white and referring to the same cell in the other worksheet. When two cells are revealed, it checks whether the numbers match or not. If the two numbers don’t match, it removes the value and covers the cell (i.e. makes the interior color green again).

Download Excel Match Pairs Simplified

 

Match Pairs La Liga

The layout of Excel Match Pairs La Liga game is a 5x5 range of cells with a total of 24 cells covered and 12 pairs to be found. The center cell (having the picture of La Liga) does not serve any purpose in the game in order to let an even number of boxes.


In this other version to match pairs in Excel, pictures of the teams of La Liga are used instead of numbers. So, when the user selects a cell, the corresponding picture is added (or more precisely, copied/pasted) to that cell. The pictures are stored in another worksheet, each fitting a cell with the same dimensions to those in the main game board and each in a different row. Then we can refer to each row number to target the picture. The name of the picture contains the row number to identify later the picture when added to the board.

 

Additionally, the cover for each cell in the main board is a shape object associated to a macro to show the hidden team when clicked. Therefore, in this version we don’t need any event procedure. The cover shapes are given a name that corresponds to the row and column where they are added. That name can be retrieved with the Application.Caller property when clicked, in order to get the row and column of the cell to uncover. The whole process consists of three steps:

  1. Delete the cover shape when clicked
  2. Select the cell that was clicked (based on the row and column index taken from the cover shape’s name)
  3. Copy/paste the corresponding picture from the other sheet (based on the random number assigned to that cell and saved to a different hidden worksheet)


We need to differentiate between the first and second selections. For that, we have an Range object variable (CardOne) declared at module level. In that way, the variable keeps the value in memory after executing the macro and shows if a cell has already been selected, otherwise the value is nothing. When selecting the second cell, the macro checks whether there is a match or not. That’s done comparing the values in the corresponding cells to CardOne against the current active cell in the configuration hidden worksheet. There is a one-second delay if the two cells don’t match, and then the pictures are deleted and shapes are added to cover the cell again.

 

There are some additional components to the match pairs game in Excel. For example, each move is registered when selecting a pair of cells. The number of pairs matched is also registered; the target in this configuration of the game is 12. Furthermore, there is a digital timer to show how long it takes to match all pairs. The timer can be stopped any time if needed.


A web version of Excel Match Pairs La Liga with similar features (and code) is available to play  online here.


Download Excel Match Pairs La Liga

 

No comments:

Post a Comment

Popular Posts