簡體   English   中英

根據其他單元格輸入鎖定Excel 2013單元格

[英]Lock Excel 2013 cells based on other cell input

我有一個工作表,其中有許多不同的列供輸入。 有問題的列是D,H和I。D和H是下拉列表。 我是任何文字輸入。 我試圖將VBA腳本放在一起,以便在D2中進行選擇時將鎖定H2和I2。 如果在H2中進行了選擇,則將鎖定D2和I2。 如果在I2中輸入文本,則將鎖定D2和H2。 最后,將需要對D,H和I的整個列進行此操作,以使這些列中的每個單元格都具有相同的屬性,即,如果I16被填充等等,D16和H16將鎖定。

如果這也可以通過公式來實現,我不介意。

  `  Private Sub Worksheet_Change(ByVal Target As Cells)
            If ActiveSheet.Cells(2, 4).Text = True Then 
'This is what I don't understand. I don't know what to set the text to. I'm trying to say if there's anything in the cell Then do the following...
                ActiveSheet.Cells(2, 8).Locked = True
                ActiveSheet.Cells(2, 9).Locked = True
            Else
                ActiveSheet.Cells(2, 8).Text = True Then
                ActiveSheet.Cells(2, 4).Locked = True
                ActiveSheet.Cells(2, 9).Locked = True
            Else
                ActiveSheet.Cells(2, 9).Text = True Then
                ActiveSheet.Cells(2, 4).Locked = True
                ActiveSheet.Cells(2, 8).Locked = True
            End If
    End Sub`

嘗試使用此代碼,但是請記住,除非工作表受到保護,否則鎖定單元格不會產生任何影響 還要注意,將ByVal Target as Range用作Worksheet_Change參數的正確語法(而不是ByVal Target as Cells )。

Private Sub Worksheet_Change(ByVal Target As Range)

If Len(Target) Then

    Select Case Target.Column

        Case Is = 4

            Me.Unprotect "pwd"
            Cells(Target.Row, 8).Locked = True
            Cells(Target.Row, 9).Locked = True
            Me.Protect "pwd"

        Case Is = 8

            Me.Unprotect "pwd"
            Cells(Target.Row, 4).Locked = True
            Cells(Target.Row, 9).Locked = True
            Me.Protect "pwd"

        Case Is = 9

            Me.Unprotect "pwd"
            Cells(Target.Row, 4).Locked = True
            Cells(Target.Row, 8).Locked = True
            Me.Protect "pwd"

    End Select

End If

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM