0

这是我的代码。这是一个功能,当我单击一个未锁定的单元格时,它会将值增加一。我在让它始终运行时遇到问题。有时它会运行,有时我必须打开代码并在最后一个 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
4

1 回答 1

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
于 2013-10-24T23:10:45.517 回答