尝试下面的代码:
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
如何使用上面的代码:将代码复制到一个空的模块到你r 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按钮应该工作了。
欢迎来到StackOverflow。请注意,这不是免费的代码写入服务。然而,我们渴望帮助其他程序员(和有志之士)编写他们自己的代码。请阅读[我如何问一个好问题](http://stackoverflow.com/help/how-to-ask)上的帮助主题。您可能还想[参观](http://stackoverflow.com/tour)并在此过程中获得徽章。之后,请用您迄今编写的VBA代码更新您的问题,以完成您希望实现的任务。 – Ralph