Spreadsheet Skunkworks! Excel VBA Chess Challenge

Can we create a chess computer in an Excel spreadsheet using Excel VBA?

Welcome to the Spreadsheet Skunkworks! In this video series, Chris tests the boundaries of what is possible in Excel. You will not find the usual step-by-step tuition here; the focus is on experimentation and creativity. Expect plenty of coding challenges, and some nice surprises about what is possible in Excel…

Skunk Snap

We will seek to use Excel’s functions and Excel VBA to create the functions of a chess computer. We do not know to what extent this is possible but, ultimately, we would like to challenge somebody to a game on Chess.com using our spreadsheet-based system. Along the way, we aim to learn lots about Excel VBA and its capabilities.

Some key questions:
– How can we represent a chess board and chess pieces in Excel?
– Can we make Excel understand where the pieces can move?
– Can we ‘teach’ Excel some simple chess principles (capture the other pieces, move your pieces to good squares), and get Excel to recommend a move to make?

What do you think about the Spreadsheet Skunkworks series? Do you have any ideas for the Chris? Don’t forget to leave a comment on YouTube.

New videos in the series will be added to this post.

Click here to go directly to the Spreadsheet Skunkworks playlist on the YouTube channel.

Video 1 – Use Excel VBA to create a chessboard effect in an Excel spreadsheet

Click here to download the completed file used in the video.

In the first video, the challenge is to create a visual ‘chessboard’ effect in Excel. Chris first looks at how to do this ‘manually’ and presents some time-saving tips. Then, we explore how to use Excel VBA to speed up what could be a time-consuming task. Chris first records some code to format a cell and employs a ‘loop within a loop’ to colour the 64 squares of the chessboard. Some VBA techniques on show include ‘do – until’ loops and conditional statements. Enjoy!

Video 2 – Use a ‘remote referencing’ approach to create a chessboard effect, and allow user control of square formats

Click here to download the completed file used in the video.

In the second video in the series, Chris improves code produced in the first. We explore how to implement a ‘remote referencing’ approach to creating the chess board which achieves efficiency improvements. We also build in a mechanism to allow the user to control the appearance of the squares, and look at some cool cell fill options. VBA techniques in the video include for / next loops, loop within a loop, integer and boolean variables, and copy / paste (special) coding. Finally, Chris introduces his new puppy, Kuda!

Video 3 – Use VBA to select a range of cells

Click here to download the completed file used in the video.

In the third video in the series, Chris deals with the topic of selecting a range of cells using VBA. This requires some intricate coding but, once the technique is mastered, its applications are powerful. Chris refers to the technique as the ‘range, range, range!’ technique because three components are required – Range(Range 1, Range 2); range 1 refers to the top left-corner of the range to be selected, and range 2 to the bottom-right – with all cells between the two ranges being selected. See how Chris applies this technique in the video to quickly change the formatting of the number / letter references for the chess board. Chris also discusses how to best represent the chess pieces in Excel – using cells, or shapes?

Video 4 – For Next Loop, Offset and Variables to create and position pieces

Click here to download the completed file used in the video.

We currently have a chess board, but no pieces! In the fourth video in the series, we explore how to create the ‘pieces’, and position them accurately. It would be possible to do this ‘manually’ using shapes and dragging them around the board; but, it would be better to automate this process with a ‘reset’ button, to set the pieces up for a new game.

What coding techniques are required to get this job done? Chess comprises 32 pieces in total, so we’ll use a loop, combined with a table in the spreadsheet, to repeat the ‘positioning’ process while changing important details for each piece. Some Tiger favourites are on show here, including the offset command and integer variables. We’ll also create a ‘backend’ sheet to hold information that the user does not need to see.

Video 5 – The Power of Loops

Click here to download the completed file used in the video.

You could say that the last video was heavy in terms of coding, and light in terms of what we achieved in a practical sense. But, this is typical of most coding tasks: intensive programming is required at the beginning to set up the various mechanisms but, once the foundation is in place, we can quickly ‘scale up’ the code to get jobs done quickly. This is why Chris describes coding tasks as ‘front-loaded’.

In the fifth video, we firm up the foundation and then ‘scale up’ the code by populating a table in the backend, and adjusting the loop control to iterate through the code multiple times. This allows us to generate a row of pawns, positioned precisely on the board. What facilitates this awesome click-of-a-button functionality? We are witnessing the power of loops…

Chris also talks about the important ‘subset’ of skills in Excel VBA that create disproportionate value for the programmer. There is so much to learn in VBA, and in Excel more generally. Chris argues in the video that a small number of techniques, combined together, is all that is needed to create awesome Excel applications. What are some of the main components of this ‘subset’? If you follow Tiger’s videos, you will know that loops, variables and offset feature prominently.

Video 6 – Organising Code and Harnessing a Loop

Click here to download the completed file used in the video.

The next video starts with the ‘dry’ topic of code organisation in the VBA editor. Code is usually organised into ‘modules’ in the VBA editor; well-organised consists of routines (‘macros’) that are grouped by theme, and located in named modules. Moreover, as the name of a routine should describe its function, the name of a module should give us a clue about what the routines inside it do. As more routines are created, it is important to periodically step and back and tidy things up. Yes, things might be understandable to you, but would a colleague or a customer be able to make sense of things? What about you in six months’ time?! A professional coder allocates time to keeping things neat-and-tidy, and understands that, in the long run, this approach is more time-efficient anyway.

Now, the exciting stuff! In this video, Chris creates the starting position for a chess game, which consists of 32 pieces positioned on the correct squares. This task sounds onerous but, by harnessing the loop created in the previous video, we can get it done ‘at the click of a button’. The key mechanism is the interaction between the visual basic editor and a table on the ‘Engine’ sheet in the Excel file. It is easy to type information into the table; looping through a set of instructions 32 times, the VBA editor takes this information and, via a set of variables, uses it to position objects on the spreadsheet. The video shows how, with the basic ‘for-next loop’ mechanism established, things can be ‘scaled up’ very quickly to get a mind-boggling amount of work done.

As a bonus, Chris shows how to use text formulae such as =RIGHT, =LEFT and =LEN to quickly manipulate text, and avoid excessive typing.

Now, we’re almost ready for a game of chess!

What do you make of the spreadsheet skunkworks series? Any questions about Excel VBA? We would love to hear from you. Contact us here.

This entry was posted in Advanced Articles, Excel Guidance Articles, Excel Guidance Videos & Articles, Intermediate Articles, Uncategorized. Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.