2014-11-14 47 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

感謝您抽出時間來解決我的問題的時間。 –

相關問題