用户表单输入后锁定单元格

Lock cells after userform input

我需要在提交用户表单后锁定行和单元格。

当您在用户窗体中插入数据时,这些数据会转到名为“table”的选项卡。我需要锁定选项卡 TABLE 并只允许用户输入。

我需要锁定从 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