当某人使用Lorenzo的工作簿时,他希望用户只能访问F2键。他想知道是否有一种方法可以禁用Excel中的所有功能键(F2键除外)?

有一种方法,但是必须使用宏来完成。具体来说,您将要使用Application对象的OnKey方法。该代码将禁用除F2之外的所有功能键:

Private Sub Workbook_Open()

Application.OnKey "{F1}", ""

Application.OnKey "{F3}", ""

Application.OnKey "{F4}", ""

Application.OnKey "{F5}", ""

Application.OnKey "{F6}", ""

Application.OnKey "{F7}", ""

Application.OnKey "{F8}", ""

Application.OnKey "{F9}", ""

Application.OnKey "{F10}", ""

Application.OnKey "{F11}", ""

Application.OnKey "{F12}", ""

End Sub

宏必须添加到ThisWorkbook模块中,并且在打开工作簿时它将自动运行。

如果需要,可以使宏短一些:

Private Sub Workbook_Open()

Dim sTemp As String     Dim J As Integer

For J = 1 to 12         If J <> 2 Then             sTemp = "{F" & Trim(Str(J)) & "}"

Application.OnKey sTemp, ""

End If     Next J End Sub

您还需要确保在关闭工作簿时重新启用所有功能键。同样,该宏应添加到ThisWorkbook模块中:

Private Sub Workbook_BeforeClose(CANCEL As Boolean)

Application.OnKey "{F1}"

Application.OnKey "{F3}"

Application.OnKey "{F4}"

Application.OnKey "{F5}"

Application.OnKey "{F6}"

Application.OnKey "{F7}"

Application.OnKey "{F8}"

Application.OnKey "{F9}"

Application.OnKey "{F10}"

Application.OnKey "{F11}"

Application.OnKey "{F12}"

End Sub

您也可以使用相同代码的较短版本:

Private Sub Workbook_BeforeClose(CANCEL As Boolean)

Dim sTemp As String     Dim J As Integer

For J = 1 to 12         If J <> 2 Then             sTemp = "{F" & Trim(Str(J)) & "}"

Application.OnKey sTemp         End If     Next J End Sub

_ExcelTips_是您进行经济高效的Microsoft Excel培训的来源。

本提示(13523)适用于Microsoft Excel 2007、2010、2013、2016、2019和Office 365中的Excel。