我对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")
实现此目的的最佳方法是什么(userform或CommandButton点击)?你有什么例子吗?
到目前为止我所写的内容:
工作表区
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
答案 0 :(得分:0)
这并不是你所要求的,但也许这辆面包车可以帮到你。对于此代码,您需要在某处放置一个简单的按钮,如果您选择了要放置船只的单元格,请按此按钮。
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
这是一个仅在输入绿色或红色时才有效的示例。我想你可以自己做别人。
我希望这有助于:)