任何人都可以帮助我。我正在使用excel来创建一个曲棍球模拟器,我需要一种方法(可能是一个按钮?)来从游戏时钟中抽出时间。
例如,单元格D4将在20:00开始,我需要一个按钮才能起飞:每次点击它时,距离时钟20(20秒)。所以点击,时间将显示19:40,然后再次点击按钮,它将显示19:20,然后点击和19:00,然后点击和18:40,等等...一直到0:00。然后它需要在下一个时段重置回20:00。
我认为一个按钮可以工作,但如果有一种不同的方式我会感兴趣,只要它让游戏时钟下降:每次我需要它下降20秒。
答案 0 :(得分:0)
您可以从excel中的开发人员菜单/工具箱中将该按钮插入电子表格。
您需要使用[m]:ss
将单元格格式化为自定义然后在您的按钮代码中,您可以使用:
Sub ChangeGameClock()
Range("D4").Value = DateAdd("s", -20, Range("D4").Value)
If Range("D4").Value = "0" Then
Range("D4").Value = "0:20:00"
End If
End Sub
祝你好运
答案 1 :(得分:0)
请尝试以下代码:
Option Explicit
Public Sub tmpSO()
'Set the cell to work on as cell D4 on the sheet
' with the name Sheet1. You might have to change
' the sheet name if your sheet is named differently.
Dim cell As Range
Set cell = ThisWorkbook.Worksheets("Sheet1").Cells(4, "D")
'Remove 20 seconds from D4
cell.Value2 = DateAdd("s", -20, cell.Value2)
's stands for seconds as outlined here:
'https://msdn.microsoft.com/en-us/library/office/gg251759(v=office.15).aspx
'If D4 is 0 or less then it should be reset to 20 minutes
If cell.Value2 <= 0 Then cell.Value2 = TimeSerial(0, 20, 0)
End Sub
如何使用上述代码:将代码复制到Excel文件中的空模块中。然后,您可以在工作表中添加一个新的(表单控件)按钮,并将上面的宏(当前名为tmpSO
)分配给该按钮。如果您使用的是ActiveX按钮,则应在插入按钮后双击该按钮。然后VBE会弹出这样的东西:
Option Explicit
Private Sub CommandButton1_Click()
End Sub
只需将其更改为
即可Option Explicit
Private Sub CommandButton1_Click()
'Set the cell to work on as cell D4 on the sheet
' with the name Sheet1. You might have to change
' the sheet name if your sheet is named differently.
Dim cell As Range
Set cell = ThisWorkbook.Worksheets("Sheet1").Cells(4, "D")
'Remove 20 seconds from D4
cell.Value2 = DateAdd("s", -20, cell.Value2)
's stands for seconds as outlined here:
'https://msdn.microsoft.com/en-us/library/office/gg251759(v=office.15).aspx
'If D4 is 0 or less then it should be reset to 20 minutes
If cell.Value2 <= 0 Then cell.Value2 = TimeSerial(0, 20, 0)
End Sub
之后,您的ActiveX按钮也应该正常工作。