如何禁用鼠标只能通过VBA为特定的Excel工作表单击?

我正在做一个项目,要求不要让用户使用鼠标从一个单元格移动到另一个单元格,他们只能通过Tab键移动。 所以,我不知道如何禁用鼠标点击特定的Excel表格,并允许用户只使用Tab键。

提前致谢!

在这个答案中,我想你的目标不是locking编辑单元格,而只是禁止使用鼠标select单元格。 也就是说,用户仍然可以编辑一个单元格,但是她需要先使用Tab或者Arrowbutton来select它,然后她可以通过按F2或者键入来编辑它。

将下面的代码添加到您的工作表的代码模块:

 Option Explicit Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer ' The keyword PtrSafe is needed only if you are working on a Win64 platform ' Remove it if you are using Win32 Private lastSelection As Range Private Sub Worksheet_Activate() On Error Resume Next Set lastSelection = Selection.Cells(1) If Err.Number <> 0 Then Set lastSelection = Cells(1, 1) End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim e As Variant For Each e In Array(vbKeyUp, vbKeyDown, vbKeyLeft, vbKeyRight, vbKeyTab, vbKeyReturn, vbKeyHome, vbKeyEnd, vbKeyPageDown, vbKeyPageUp) If CBool(GetAsyncKeyState(e) And &H8000) Then 'You got here without using the mouse Set lastSelection = Target ' do other stuff if needed Exit Sub End If Next ' Selection was changed via mouse. Rollback If lastSelection Is Nothing Then Set lastSelection = Cells(1, 1) lastSelection.Activate End Sub Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Cancel = True End Sub 

几个选项

1.不保护表格 – 强制所有点击单元格A1(在表单的模块中)

 Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False With Target If .Column <> 1 Or .Row <> 1 Or .CountLarge > 1 Then Application.Undo End With Application.EnableEvents = True End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Application.EnableEvents = False With Target If .Column <> 1 Or .Row <> 1 Or .CountLarge > 1 Then Cells(1, 1).Select End With Application.EnableEvents = True End Sub 

2.保护表格 – 不允许单元格导航

 Private Sub Worksheet_Activate() Me.Protect Me.EnableSelection = xlNoSelection End Sub