簡體   English   中英

用戶表單輸入后鎖定單元格

[英]Lock cells after userform input

我需要在提交用戶表單后鎖定該行和單元格。

當您在用戶表單中插入數據時,這些數據 go 到名為“表”的選項卡中。 我需要鎖定選項卡表並只允許用戶表單輸入。

我需要從 A4 到 AF4 的行和單元格被鎖定以進行編輯。

我嘗試使用此代碼。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRange As Range

Set MyRange = Intersect(Range("A1:D100"), Target)
If Not MyRange Is Nothing Then
    Sheets("Sheet1").Unprotect password:="hello"
    MyRange.Locked = True
    Sheets("Sheet1").Protect password:="hello"
End If
End Sub

這就是我的命令按鈕的外觀

Private Sub CommandButton2_Click()
Dim sh As Worksheet, lastRow As Long
Set sh = Sheets("Details")lastRow = sh.Range("A" & Rows.Count).End(xlUp).row + 1
sh.Range("A" & lastRow).value = TextBox3.value
sh.Range("B" & lastRow).value = TextBox4.Text
sh.Range("C" & lastRow).value = TextBox5.Text
Unload Me
End sub

首先,手動鎖定A4:AF[ChooseTheLastRow]中的單元格,然后使用密碼保護工作表,不允許選擇鎖定的單元格。

然后在您的代碼中執行此操作。

Private Sub CommandButton2_Click()

  Dim sh As Worksheet
  Set sh = Sheets("Details") 'you called this TABLE in your text above, no?

  With sh

      .unprotect "PASSWORD"

      Dim lastRow As Long
      lastRow = .Range("A" & Rows.Count).End(xlUp).row + 1

      .Range("A" & lastRow).value = TextBox3.value
      .Range("B" & lastRow).value = TextBox4.Text
      .Range("C" & lastRow).value = TextBox5.Text

      .protect "PASSWORD"

  End With

End sub

暫無
暫無

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

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