ICAEW.com works better with JavaScript enabled.
Exclusive

From numbers to pictures – using INDEX() with MAKEARRAY()

Author: Simon Hurst

Published: 13 Dec 2024

Exclusive content
Access to our exclusive resources is for specific groups of students, subscribers and members.

Having in the previous post used several of the new LAMBDA() helper functions, most notably MAKEARRAY(), to calculate the total number of gifts given across the 12 days of Christmas, this time we are going to replace our numbers with pictures that represent each of the gifts.

Introduction

Last time, we looked at calculating the total number of presents donated over the 12 days of Christmas according to the festive song. As part of creating the required formula, we used the MAKEARRAY() function to create a visual representation of the calculation in a 12 by 12 matrix:

Screenshot from an Excel spreadsheet

As useful as the matrix might be in visualising the problem, it could certainly be improved by replacing the dull numbers with appropriate pictures. It would be possible to do this using pictures in cells, and formulas, but it would be a bit cumbersome. Instead, we could make a relatively simple addition to our MAKEARRAY() formula. We are going to use the INDEX() function which can return elements of a set, such as cells in a column, by position.

First of all, we need to create our twelve pictures. We have used Microsoft Paint with its new generative AI capabilities to create the pictures, with impressive results for the most part, and save them.

We have then used the Insert Ribbon tab, Illustrations, Pictures, Place in Cell option to choose This Device…

Screenshot from an Excel spreadsheet

INDEX() allows us to refer to the cells containing the pictures by their position in the column, using the numbers 1 to 12 – the same numbers that we already have displayed in our grid.

The first argument of the INDEX() function is the range of cells containing the pictures. The second INDEX() argument is the index number representing the position, in our case this is a number from 1 to 12 to correspond with the number of cells in our range. We are using our MAKEARRAY() function to provide these numbers in our grid format:

=INDEX($P$7:$P$18,MAKEARRAY(GiftDays,GiftDays,LAMBDA(r,c,r*(c>=r))))

Unfortunately, this doesn’t quite work. Although the numbers from 1 to 12 do work, and do put the pictures in the correct places, where our MAKEARRAY() function returns 0, INDEX() still seems to return picture 1:

Screenshot from an Excel spreadsheet
In fact, INDEX(), with the position number set to 0, actually returns the entire array of cells and, because this is already part of an array, it can only display the first item in the array. We can see this by using the INDEX() function on its own:
Screenshot from an Excel spreadsheet

One solution is to add 1 to each index number so that our grid contains the numbers 1 to 13, rather than 0 to 12. We can then extend our picture array to include a blank cell at the top of the array. The cells that previously displayed 0 will now display 1. Used as part of INDEX(), 1 will now return the contents of the empty cell, with index numbers 2-13 returning the pictures for days 1 to 12.

=INDEX($P$6:$P$18,MAKEARRAY(GiftDays,GiftDays,LAMBDA(r,c,r*(c>=r)+1)))

Screenshot from an Excel spreadsheet

We now have our grid of gift pictures and potentially the answer to several other Christmas quiz questions.

Not just for Christmas…

This technique has a wider application than answering Christmas quiz questions. Using INDEX() with a list within a dynamic array formula can play a part in generating random data for testing or similar purposes. In this example, we have used a variety of Excel dynamic array formulas to populate a range of cells with random values:

=HSTACK(RANDARRAY(A2,1,B2,B3,TRUE),INDEX($C$2:$C$10,RANDARRAY
(A2,1,1,COUNTA($C$2:$C$10),TRUE)),RANDARRAY(A2,1,D2,D3,TRUE))

Screenshot from an Excel spreadsheet

Additional resources

You can explore all aspects of Excel, including other Christmas special series, in the ICAEW archive:

Archive and Knowledge Base

This archive of Excel Community content from the ION platform will allow you to read the content of the articles but the functionality on the pages is limited. The ION search box, tags and navigation buttons on the archived pages will not work. Pages will load more slowly than a live website. You may be able to follow links to other articles but if this does not work, please return to the archive search. You can also search our Knowledge Base for access to all articles, new and archived, organised by topic.

Open AddCPD icon

Add Verified CPD Activity

Introducing AddCPD, a new way to record your CPD activities!

Log in to start using the AddCPD tool. Available only to ICAEW members.

Add this page to your CPD activity

Step 1 of 3
Download recorded
Download not recorded

Please download the related document if you wish to add this activity to your record

What time are you claiming for this activity?
Mandatory fields

Add this page to your CPD activity

Step 2 of 3
Mandatory field

Add activity to my record

Step 3 of 3
Mandatory field

Activity added

An error has occurred
Please try again

If the problem persists please contact our helpline on +44 (0)1908 248 250