VBA-在工作表上添加不同的船舶

VBA-在工作表上添加不同的船舶,vba,excel,Vba,Excel,我是VBA的新手。我试图创建一个简单的战舰游戏。当用户点击游戏区域时,我想创建一个弹出窗口,允许我插入不同的元素 例如: Ship 1 (length 2 columns & colour "red") Ship 2 (length 3 columns & colour "blue") Ship 3 (length 4 columns & colour "black") Ship 4 (length 5 columns & colour "green") 实现这

我是VBA的新手。我试图创建一个简单的战舰游戏。当用户点击游戏区域时,我想创建一个弹出窗口,允许我插入不同的元素

例如:

Ship 1 (length 2 columns & colour "red")
Ship 2 (length 3 columns & colour "blue")
Ship 3 (length 4 columns & colour "black")
Ship 4 (length 5 columns & colour "green")
实现这一点的最佳方法是什么(用户表单或命令按钮单击)?你有什么例子吗

这就是我到目前为止写的:

工作表区域

Private Sub CommandButton1_Click()
Dim chemin1 As Object
Set chemin1 = Application.ThisWorkbook.Worksheets(1)
With chemin1
    .Clear
    .Range("A1:J10").Interior.ColorIndex = 36
With Range("A1:J10").Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1:J10").Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1:J10").Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1:J10").Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1:J10").Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    End With

End Sub
在工作表中添加船舶的操作

Private Sub CommandButton1_Click()
Dim LB_2 As Variant, LB_3, LB_4 As Variant, LB_5 As Variant
Dim LB_6 As Variant
Set chemin1 = Application.ThisWorkbook.Worksheets(1)
With chemin1
LB_2 = TB_1.variant
.Range("TB_1").Interior.ColorIndex = 22
LB_3 = TB_2.variant
.Range("TB_2").Interior.ColorIndex = 22
LB_4 = TB_3.variant
.Range("TB_3").Interior.ColorIndex = 22
LB_5 = TB_4.variant
.Range("TB_4").Interior.ColorIndex = 22
LB_6 = TB_5.variant
.Range("TB_5").Interior.ColorIndex = 22
End With

End Sub

Private Sub TextBox1_Change()

End Sub

这不是你想要的,但也许这辆车能让你走。对于这段代码,您需要在某个地方放置一个简单的按钮,如果您选择了要放置飞船的单元格,请按下该按钮

Option Explicit
Sub battleship()
Dim length As Integer
Dim color As String
    length = InputBox("Please give the length of your ship")
    color = InputBox("Please give the color of your ship")
    If color = "red" Then
        Do While length > 0
            length = length - 1
            ActiveCell.Interior.color = RGB(255, 0, 0)
            ActiveCell.Offset(1, 0).Select
        Loop
    ElseIf color = "green" Then
        Do While length > 0
            length = length - 1
            ActiveCell.Interior.color = RGB(0, 255, 0)
            ActiveCell.Offset(1, 0).Select
        Loop
    End If
End Sub
此示例仅在输入绿色或红色时有效。我想你可以自己做其他的


我希望这有帮助:)

选择您喜欢的方法,尝试编写一些代码,然后在遇到问题时用代码发回。我知道这完全不同,但我希望这会帮助您。这对我帮助很大!谢谢