Sunday, February 24, 2019

Excel Rubik Cube Puzzle

In this post we see how to create a fun game inspired in Rubik’s Cube with Excel VBA. It’s a very simple macro that gives a chance to go through some formatting properties. Additionally, we will learn to use Events to add some dynamic features to the game. Excel Rubik Cube Puzzle comes with three levels of difficulty and follows some of the algorithms the real cube does.


The Layout

The “FormatLayout” subroutine applies some VBA formatting properties to cells and ranges in order to create the Excel Rubik Cube Puzzle grid. The code consists of several blocks of With / End With statements for the different range objects to be formatted. Additionally, validation to select the level of difficulty is added. That macro just formats the layout as it appears initially in the main worksheet. It should not be used again unless the layout is altered for some reason. This piece of code is not linked to the other Excel Rubik Cube Puzzle game subroutines.

Please note the headings and formula bar are being removed. If you wish to restore them you may go to View-Show and tick the respective boxes, or set the corresponding VBA properties to True as indicated in the previous post.

Start New Game

The “NewCube” subroutine randomly adds each of the colours to a grid with the number of cells correspondent to the selected level: 4x4 cells grid for “Easy”, 6x6 cells grid for “Intermediate”, and 8x8 cells grid for “Hard”. For that purpose, we use the Randomize/Rnd VBA functions and a For/Next loop through the four basic colors: ColorIndex values of 3 for red, 4 for green, 5 for blue and 6 for yellow. Remember the value for white is 2, black 1 and transparent 0.

In order to manipulate the Excel Rubik Cube Puzzle we use Form Controls with arrows that allow to roll an entire row or column in one or the opposite direction based on the selected or active cell. Each arrow is linked to a movement subroutine, which captures active row and column numbers with ActiveCell.Row and ActiveCell.Column respectively. Then it checks if the selection lays within the cube grid and moves the selection in the desired direction by a combination of copy/pasting cells’ interior colors into and from a non-visible worksheet range selection.

The “Solution” worksheet shows the different possible combinations the player needs to aim for in order to solve the Excel Rubik Cube Puzzle.

 

Active Selection

Excel Rubik Cube Puzzle uses a VBA Event to show the selected row and column upon clicking on any cell within the cube grid. That’s the SelectionChange Event of the Worksheet Object, which is written within the given worksheet VBA window, in this case, Sheet1 (Cube).


Once the Event is triggered, the VBA code captures active row and column numbers as explained before and adds a border to the corresponding row and column with the BorderAround property. Events code The SelectionChange Event is triggered with any change within a cell of the given worksheet and it is described as follows:

 
  Private Sub Worksheet_SelectionChange(ByVal Target As Range)
--- code to add border to the selection
  End Sub
 


Adding or formatting borders around a given range is achieved by:

 
  With Worksheets(1).Range(“A1:D4”)
.BorderAround LineStyle:=xlContinuous, Weight:=xlThick, Color:=vbBlack
  End With
 


 
Adding or formatting borders to all cells within a range is achieved by:

 
  With Worksheets(1).Range(“A1:D4”)
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThick
.Borders.Color = vbWhite
  End With
 

 
 

2 comments:

  1. Hello Excel Macro MAniac,
    I'm having a hard time getting to you google account to see all the code. I have a class of special students that would love to write this out in their own excel.

    ReplyDelete
  2. Hello Lightamir, you can find the VBA code within the downloadable excel file in this post that is accessible to anyone (if that doesn't work, you may try this link: https://drive.google.com/file/d/1F2f-FWErQBRs1Jy6MWHV4zyJMMM4VHgy/view?usp=sharing). If you have any other questions you can reach me at XlsGsData@gmail.com

    ReplyDelete

Popular Posts