我在工作表中运行私有子。我可以点击某些内容来启用/禁用它吗?即我可以编程一个按钮吗?

时间:2013-10-24 22:32:58

标签: excel vba

这是我的代码。它是一个函数,当我单击一个未锁定的单元格时,它会将值增加一。我遇到了让它始终运行的问题。有时它会运行,有时我必须打开代码并在最后一个END SUB后按Enter键。我想要的是一个刷新此代码的按钮,以便VBA工作。

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     Dim Key As Integer

     If Target.Count > 1 Then Exit Sub

     Key = GetKeyState(MOUSEEVENTF_LEFTDOWN)

     If Key And 1 Then
        If IsNumeric(Target.Value) Then
           Target.Value = Target.Value + 1
           Application.EnableEvents = False
           Target.Resize(1, 2).Select
           Application.EnableEvents = True
        End If
        Cancel = True
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

您如何看待这个适用于我的简化代码?

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Cancel = True
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

  If Target.Count > 1 Then Exit Sub

  If IsNumeric(Target.Value) Then
    Application.EnableEvents = False
    Target.Value = Target.Value + 1
    Target.Resize(1, 2).Select
    Application.EnableEvents = True
  End If
End Sub