2014-11-14 38 views
0

我面临着使用Excel宏语法的复杂问题。 我的工作簿包含几张纸,第一张标题为“参考纸”。 其他工作表中有一些条目,我不希望用户通过他们当前正在处理的工作表进行编辑,但只能通过参考工作表进行编辑。在另一个工作表中选择当前活动的单元格

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

我的目标是在当前工作表中选择相同的单元格,并在参考工作表中进行选择以便对其进行编辑。

我张贴以下代码中的对应片VB但显然有在端部中的细胞特性的错误 - >细胞(VAL1,VAL2)。选择

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 

你的想法是十分赞赏。

回答

1

原因您的代码失败的是,在一个Worksheet模块不合格范围参考(Cells(val1, val2)你的情况)是指在工作表模块,而不是主动工作表。由于您刚刚激活了另一张工作表,因此您正尝试在不活动工作表上选择一个导致错误的单元格。

一个更好的办法来做到这一点是

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 
+0

感谢您抽出时间来解决我的问题的时间。 –

相关问题