Within the AddShip subroutine the coordinates of the shape are calculated based on the selected range and then the rounded rectangle shape is added see below.
Width - 4. Height - 4. AddShape msoShapeRoundedRectangle, x, y, w, q. In order to show the shape we need to add the following properties:. With newShip. End With. And this will make the border of the rectangle rounded by 10 degrees:.
The Movement. Row and ActiveCell. Column respectively, and shows a message box for the player to confirm the selected coordinates in Excel Battleship. Then it goes through a number of IF statements to check if ship was hit by looking at the SolutionWS worksheet. Enjoy playing Excel Battleship and share any comments below. Download Excel Battleship. Add an Else clause which changes the fill colour to red and shows a message if the cell isn't empty. You could expand the message to say what type of ship you hit.
The game won't work if a user attempts to select multiple cells. Add an If statement at the start of the subroutine which checks if the Count or CountLarge property of the target is greater than 1. If so, display a message and exit the subroutine. You could add a separate subroutine to reset the game by changing the ColorIndex property of the cells to xlNone. Outstanding course feedback Average score 9. Thanks for trying our exercises! Recording macros and buttons 6 Selecting cells 11 Messages and variables 4 Visual Basic explained 3 Loops and conditions 5 Looping over collections 11 Event handling 4 Functions 1 Debugging and error handling 1 Forms 4 Referencing other applications 2.
Excel VBA Macros Event handling exercise Create a Basic Battleships Game using Worksheet Events This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
Before you can do this exercise, you'll need to download and unzip this file if you have any problems doing this, click here for help. Use the drop down lists at the top of the code window. Test your code works by clicking on a cell. Your message could resemble this one.
0コメント