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).
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
Hello Excel Macro MAniac,
ReplyDeleteI'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.
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