将START和STOP按钮添加到现有代码中

时间:2015-10-22 16:17:50

标签: excel vba timer

在A栏中输入一个值。 首先,双击A列中的值。 要停止,请双击任何空白单元格。 停止在B2单元格中运行。

Public stopMe As Boolean
Public resetMe As Boolean
Public myVal As Variant


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 Then
        If Target.Value = myVal And Target.Value <> "" Then
            'Changed
            Dim startTime, finishTime, totalTime, timeRow
            startTime = Timer
            stopMe = False
            resetMe = False
            myTime = Target.Offset(, 2).Value
            Target.Offset(, 1).Select
startMe:
            DoEvents
            timeRow = Target.Row
            finishTime = Timer
            totalTime = finishTime - startTime
            Target.Offset(, 1).Value = Format(800 + totalTime, "0000")
            If resetMe = True Then
                Target.Offset(, 1).Value = 0
                Target.Offset(, 2).Value = 0
                stopMe = True
            End If
            If Not stopMe = True Then
                Target.Offset(, 2).Value = totalTime
                GoTo startMe
            End If
            Cancel = True
            End
        Else
            'Not Changed
            stopMe = True
            Cancel = True
        End If
    End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    myVal = Target.Value
End Sub

我想制作启动,停止和按钮的按钮重置并链接此代码但无法执行此操作。

1 个答案:

答案 0 :(得分:1)

以下是一些创建按钮的代码。在工作表上有按钮后,将它们移动到所需位置,调整大小并为其分配代码:

Sub ThreeButtons()
   ary = Split("Start,Stop,Reset", ",")
   t = 10
   For i = 1 To 3
      Set shp = ActiveSheet.Shapes.AddShape(5, 100, t, 60, 60)
      shp.TextFrame.Characters.Text = ary(i - 1)
      shp.ShapeStyle = msoShapeStylePreset & (40 - i)
      t = t + 70
   Next i
End Sub

enter image description here

改编自:peltiertech