强制用户只select一个特定的单元格

我无法locking我的工作表,因为我需要访问一些单元格来运行macros。

我想要一个macros,select单元格(F1),每当用户尝试select任何其他单元格。

我需要一个这样的macros,我猜:

Private Sub Worksheet_Change(ByVal Target As Range) End Sub 

将这段代码放在工作表模块中:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Call Cells(1, 6).Select End Sub 

像其他人一样写,最好是解除工作表的保护,播放你的macros,保护工作表,但是,如果你认为禁用所有的单元格,减去你确定的单元格是最好的方法,你可以使用下面的代码:

 Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False If Not Intersect(Target, Range("F10")) Is Nothing Then 'use your free cell do Else Application.Undo MsgBox "you can modify only the cell(F10)" 'Just to inform what is the cell editable Range("F10").Select End If Application.EnableEvents = True End Sub 

这里有两个选项:

1.不保护纸张 – 在每个纸张模块中定制“解锁”单元(例如“C3”)


 Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False With Target If .Column <> 3 Or .Row <> 3 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 <> 3 Or .Row <> 3 Or .CountLarge > 1 Then Cells(3, 3).Select End With Application.EnableEvents = True End Sub 

2.保护表格 – 在ThisWorkbook模块中


 Private Sub Workbook_Open() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets protectWS ws Next End Sub Private Sub Workbook_SheetActivate(ByVal Sh As Object) protectWS Sh End Sub Public Function protectWS(Optional ByRef ws As Worksheet = Nothing) As Boolean If ws Is Nothing Then Set ws = Application.ActiveSheet If Not isWSProtected(ws) Then ws.Protect Password:=vbNullString, _ DrawingObjects:=True, _ Contents:=True, _ Scenarios:=True, _ UserInterfaceOnly:=True, _ AllowFormattingCells:=False, _ AllowFormattingColumns:=False, _ AllowFormattingRows:=False, _ AllowInsertingColumns:=False, _ AllowInsertingRows:=False, _ AllowInsertingHyperlinks:=False, _ AllowDeletingColumns:=False, _ AllowDeletingRows:=False, _ AllowSorting:=False, _ AllowFiltering:=False, _ AllowUsingPivotTables:=False End If End Function Private Function isWSProtected(Optional ByRef ws As Worksheet = Nothing) As Boolean isWSProtected = ws.ProtectContents Or _ ws.ProtectDrawingObjects Or _ ws.ProtectScenarios End Function 

  • 选项1强制用户到单元格C3

    • 可以移动到ThisWorkbook模块,使用行和列的参数
  • 选项2使用空密码,并允许VBA执行( UserInterfaceOnly