2014-10-30 65 views
2

我需要鎖定基礎上的價值從列表從另一個單元中的選擇細胞的特定範圍幫助細胞的選擇範圍。需要幫助鎖定使用VBA

具體來說,我已經創建了列N5到N36的數據驗證列表,在從單元格N5中選擇值「存在」後,我想將該特定行O5鎖定到U5。

即「存在」,在N6將鎖定O6到U6等。

同樣,對於其他行至N36。

而且,如果用戶選擇「不存在」,那麼我會希望這些細胞保持未鎖定和編輯類似於上述條件。

我已經試過宏使用我的使用宏的非常基本的知識各種論壇,但那些最鎖定整個表。

代碼我想:

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Not Intersect(Target, Range("N5:N36")) Is Nothing Then 
     ActiveSheet.Unprotect 

     If Target.Value = "Exist" Then 
      Range("O" & Target.Column & ":U" & Target.Column).Select Selection.Locked = False 
     Else 
      Range("O" & Target.Column & ":U" & Target.Column).Select Selection.Locked = True 
     End If 
    End If 

    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True 
End Sub 

我會很感激你的快速幫助。

在此先感謝。

+0

你應該解開不在這個範圍內,然後保護工作表的所有單元格,我會考慮的代碼一點點;) – 2014-10-30 07:43:13

+0

嗨@GoosvandenBekerom, 下面是我使用的代碼之一實際上,在我從一個單元格中選擇後,整個表格都被鎖定。 – 2014-10-30 07:50:59

+0

私人小組Worksheet_Change(BYVAL目標作爲範圍) 如果沒有相交(目標,範圍( 「N5:N36」))是沒有那麼 ActiveSheet.Unprotect 如果Target.Value = 「存在」 然後 範圍(」 (「O」&Target.Column&「:U」&Target.Column)。選擇 選擇.Locked = True End If End If ActiveSheet.Protect DrawingObjects:= True,內容:= True,方案:= True End Sub – 2014-10-30 07:53:12

回答

2

這是你想什麼(久經考驗)?另見THIS。值得一讀。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim rw As Long 
    Dim sPass As String 

    '~~> Password 
    sPass = "BlahBLah" 

    On Error GoTo Whoa 

    '~~> For excel 2003 use .Count instead of .CountLarge 
    '~~> In case of multiple cells were changed   
    If Target.Cells.CountLarge > 1 Then Exit Sub 

    Application.EnableEvents = False 

    If Not Intersect(Target, Range("N5:N36")) Is Nothing Then 
     If UCase(Trim(Target.Value)) = "EXIST" Then 
      rw = Target.Row 

      With ActiveSheet 
       .Unprotect sPass 

       .Cells.Locked = False 

       .Range("O" & rw & ":U" & rw).Locked = True 

       .Protect Password:= sPass , DrawingObjects:=True, _ 
       Contents:=True, Scenarios:=True 
      End With 
     End If 
    End If 

Letscontinue: 
    Application.EnableEvents = True 
    Exit Sub 
Whoa: 
    MsgBox Err.Description 
    Resume Letscontinue 
End Sub 
+0

感謝您幫助悉達思。但是,當我運行代碼時,我遇到了一個錯誤,因爲「編譯錯誤預期子,函數或屬性」。請建議如何進一步處理。再次感謝。 – 2014-10-30 10:46:14

+0

啊好的。我知道了...查看更新後的帖子。您可能需要刷新頁面才能看到它 – 2014-10-30 11:45:38

+0

工程就像一個魅力。非常感謝Siddharth的幫助。你能否讓我知道你所做的改變,這將有助於我理解。再次感謝。 – 2014-10-30 12:39:43

0

你可以做這樣的事情:

Sub LockCells() 

    'unprotect the sheet 
    ActiveSheet.Unprotect 

    'unlock all cells 
    Cells.Locked = False 
    Cells.FormulaHidden = False 

    Dim cell As Range 

    'find all cells that need to be locked 
    For Each cell In Range("N5:N36") 
     If cell = "Exist" Then 

      Range("O" & cell.Row & ":U" & cell.Row).Locked = True 
      Range("O" & cell.Row & ":U" & cell.Row).FormulaHidden = True 

     End If 
    Next cell 

    'protect the sheet 
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True 

End Sub 
+0

將嘗試這一點,並儘快給您。謝謝你的幫助。 – 2014-10-30 07:55:46

+0

我想稍微修改一下我的問題。如果我在N5中選擇「Exist」,那麼我只想鎖定那一行,即O5到U5。對於從N5到N36的所有單獨行也是如此。請提出適當的答案。對變更抱歉。 – 2014-10-30 08:04:22

+0

對你的問題進行修改不要把它們放在評論中,就像我之前說的,改變你的問題或者我幫不了你 – 2014-10-30 08:06:47