Finance

Charts

Statistics

Macros

Search

User Selects a Puzzle Piece in Excel VBA

The procedure ClickPiece() is called whenever the user clicks one of the puzzle pieces with the mouse:

Sub ClickPiece()
    If FirstActive Then
        Set FirstPiece = ActiveSheet.Shapes(Application.Caller)
        FirstPiece.PictureFormat.Brightness = 0.25
        FirstActive = False
    Else
        FirstPiece.PictureFormat.Brightness = 0.5
        SwapPieces FirstPiece, ActiveSheet.Shapes(Application.Caller)
        Set FirstPiece = Nothing
        CheckPositions
        FirstActive = True
    End If
End Sub

Explanation:

  • The procedure first checks whether the clicked puzzle piece is the first or second piece involved in a swap operation.
  • At the end of the StartGame() procedure, just before the first swap, the Boolean variable FirstActive is set to True.
  • The Caller property of the Application object returns a reference to the object (shape) that triggered the VBA procedure — in this case, the clicked puzzle piece.
  • If FirstActive is True, the reference to the clicked puzzle piece is stored in the module-level variable FirstPiece.
  • The piece’s brightness is reduced from the default 0.5 to 0.25 via its PictureFormat.Brightness property, making it appear slightly darker.
  • This visual cue helps the user identify which puzzle piece has been selected first for swapping.
  • FirstActive is then set to False, signaling that the next clicked piece will be the second in the swap.
  • If FirstActive is False, the brightness of the first selected piece is reset back to normal (0.5).
  • The procedure SwapPieces() is called next with two parameters: references to the first selected puzzle piece and the currently clicked (second) puzzle piece. This procedure swaps their positions.
  • After each swap, the procedure CheckPositions() is called to verify whether all puzzle pieces are now correctly positioned.
  • Finally, FirstActive is reset to True to prepare for the next swap operation.
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx