SelectionChange () thủ tục sự kiện bao gồm của hai thủ tục các cuộc gọi được dựa trên các giá trị của allowSelection và biến gameStarted. Nếu allowSelection là đúng, sau đó người dùng lựa chọn các tế bào bảng vị trí tàu của mình. Nếu gameStarted là đúng, sau đó người dùng đã đặt / tàu của ông và bắn vào các mục tiêu của máy tính | Chapter 5 Basic Excel Objects ---------------------- Test if player is firing at the computer. This is first because LocatePlayerShip will turn gameStarted variable to true. ---------------------- If gameStarted Then PlayerFire Target Test if player is setting his her ships before game starts. If allowSelection Then LocatePlayerShip Target End Sub The SelectionChange event procedure consists of two procedure calls based on the values of the allowSelection and gameStarted variables. If allowSelection is true then the user is selecting worksheet cells for locating his or her ships. If gameStarted is true then the user has placed his her ships and is firing at targets on the computer s grid. It is critical that at no time during the execution of the program that the values of both variables are true. Private Sub LocatePlayerShip Target As Range Capture user s selections for ship locations. If selection is valid then color it blue and display message for next ship. Dim errMsg As String If RangeValid Target Player errMsg Then RGB 0 255 255 numShipsPlaced numShipsPlaced 1 If numShipsPlaced NUMSHIPS Then Range Output .Value Place your ships numShipsPlaced Select shipSize numShipsPlaced contiguous cells Else allowSelection False PlaceComputerShips gameStarted True Range Output .Value You may begin End If Else MsgBox errMsg End If End Sub Microsoft Excel VBA Programming for the Absolute Beginner Second Edition The LocatePlayerShip sub procedure is called from the SelectionChange event procedure and passed the user s cell selection in the form of the range variable Target. The primary goal of the LocatePlayerShip sub procedure is to validate and mark the user s selection for a ship. To accomplish this goal the Boolean value returned by the RangeValid function procedure listed later is used as the conditional statement in an If Then Else code block. If the user s selection is valid then the interior color of the selection is colored light blue and the .