Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50167

Command Button colors (userform)

$
0
0
Currently, I am working on creating a battle ship game in VBA using excel. I have made a 10 X 10 sheet of command buttons and have the following codes working (as far as the 10X 10 sheet goes):

reset of playing field
Ship Placement

The problems I am running into are:

* resetting only ONE ship. So, if a user places 4 ships, if they are out of range on the 5th one, the whole board resets. I'm using various loops because my code is set up alphanumerically.
* Checking if a ship is already placed. Once a ship is placed, the command buttons turn color based on the ship placed.

Here's a part of my code, checking for the left direction.
I am trying to figure out a way to check the color of a command button, so if it is a certain color, a ship will NOT be placed there.

Code:

y = Mid(StartingPoint, 1, 1) 'alphabet letter
z = Mid(StartingPoint, 2, Len(StartingPoint) - 1) 'number
   
If direction = "left" Then
    For i = 0 To 4 Step 1
   
        For j = 9 To 18 Step 1
       
       
        If Sheet3.Cells(j, 1).Value = y Then
        y = Sheet3.Cells(j, 2).Value
           
        End If
       
        If Sheet3.Cells(j, 1).Value = 0 Then
        MsgBox ("Out of Range")
        Battle.AirCraftCarrier.Locked = False
        Call Reset
        i = 5
        End If
       
        Next j
               
        If Sheet3.Cells(y + 1, 2).Value > 0 Then
             
            If i = 0 Then
                y = Sheet3.Cells(y - 1, 1).Value
           
                If Battle.Controls(y & z).BackColor = vbBlue Or Battle.Controls(y & z).BackColor = vbMagenta Or _
                Battle.Controls(y & z).BackColor = vbYellow Or Battle.Controls(y & z).BackColor = vbCyan Then
           
                MsgBox ("There is already a ship here!")
                Battle.Controls(y & z).BackColor = vbBlack
                End If
               
               
                'y = Sheet3.Cells(y - 1, 1).Value
                Battle.Controls(y & z).BackColor = vbMagenta
            Else
               
                y = Sheet3.Cells(y - 1, 1).Value
               
                If Battle.Controls(y & z).BackColor = vbBlue Or Battle.Controls(y & z).BackColor = vbMagenta Or _
                Battle.Controls(y & z).BackColor = vbYellow Or Battle.Controls(y & z).BackColor = vbCyan Then
           
                MsgBox ("There is already a ship here!")
                'y = Sheet3.Cells(y - 1, 1).Value
                Battle.Controls(y & z).BackColor = vbBlack
                End If
               
                'y = Sheet3.Cells(y - 1, 1).Value
                Battle.Controls(y & z).BackColor = vbMagenta
            End If
       
        Else
        MsgBox ("Out of Range")
        Battle.AirCraftCarrier.Locked = False
        Battle.Controls(y & z).BackColor = vbBlack
        'Call Reset
        i = 5
                                       
        End If
       
        For j = 9 To 18
            If Sheet3.Cells(j, 1).Value = y Then
            y = Sheet3.Cells(j, 2).Value - 1
            End If
        Next j
       
        If Sheet3.Cells(y + 1, 2).Value > 0 Then
        y = Sheet3.Cells(y - 1, 1).Value
        End If
       
        If i = 4 Then
        Battle.AirCraftCarrier.Locked = True
        End If

    Next i
       
End If

Because I am a student, it doesn't have to be super intricate. I can simply give a warning of not placing ships on top of another, but I need to be able to tell the user there is an error if they do attempt to do so.

Any help is greatly appreciated.

Viewing all articles
Browse latest Browse all 50167

Trending Articles