【问题标题】:Lock cells after userform input用户表单输入后锁定单元格
【发布时间】:2020-10-06 04:19:54
【问题描述】:

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

当您在用户窗体中插入数据时,这些数据会转到名为“表”的选项卡。我需要将 Tab 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

【问题讨论】:

    标签: excel vba user-controls userform commandbutton


    【解决方案1】:

    首先,从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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多