在另一个工作表中select当前活动的单元格

我正面临着excelmacros语法的复杂性。 我的工作簿包含几张纸,第一张标题为“参考页”。 其他工作表中有一些条目,我不希望用户通过他们目前正在处理的工作表进行编辑,但只能通过参考工作表进行编辑。

我locking了单元格并使用了保护页,但是我希望用户在双击相关范围中的一个单元格时收到提示消息,询问他们是否要更改所选单元格,但在参考表单中。

我的目标是在当前工作表中select相同的单元格,在参考页面中select,以便能够对其进行编辑。

我发布了下面的代码在相应的工作表VB中,但显然在单元格属性的结尾 – >单元格(val1,val2)有一个错误。select

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Intersect(Target, Range("C2:C5,E2")) Is Nothing Then Else Dim Msg As String, Title As String Dim Config As Integer, Ans As Integer Msg = "Do not modify this entry from the current sheet." Msg = Msg & vbNewLine Msg = Msg & "This modification is only enabled in the Reference Sheet." Msg = Msg & vbNewLine Msg = Msg & "Would you like to go to the corresponding cell and modify it?" Title = "Attention" Config = vbYesNo + vbExclamation Ans = MsgBox(Msg, Config, Title) If Ans = vbYes Then Dim val1 As Integer, val2 As Integer val1 = ActiveCell.Row val2 = ActiveCell.Column Worksheets("Reference Sheet").Activate Cells(val1, val2).Select End If End If End Sub 

你的想法非常感谢。

您的代码失败的原因是在Worksheet模块中的一个非限定范围引用(您的案件中的Cells(val1, val2) )引用工作表模块 ,而不是活动工作表。 由于您刚刚激活了另一张工作表,因此您正尝试在不活动的工作表上select一个单元格,从而导致错误。

更好的方法是

 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Intersect(Target, Range("C2:C5,E2")) Is Nothing Then Else Dim Msg As String, Title As String Dim Config As Long, Ans As VbMsgBoxResult Msg = "Do not modify this entry from the current sheet." Msg = Msg & vbNewLine Msg = Msg & "This modification is only enabled in the Reference Sheet." Msg = Msg & vbNewLine Msg = Msg & "Would you like to go to the corresponding cell and modify it?" Title = "Attention" Config = vbYesNo + vbExclamation Ans = MsgBox(Msg, Config, Title) If Ans = vbYes Then With Worksheets("Reference Sheet") .Activate .Range(Target.Address).Select End With End If End If End Sub