Excel VBA 解锁单元格以在不同工作表上运行宏,然后再次锁定

问题描述 投票:0回答:1

我有一个宏,在有人填写表单并单击提交后运行。它位于 Excel 电子表格的第一个选项卡上。然后,它会填写代码中指示的工作表上的下一个可用行。问题是我希望能够保护它正在写入的一些单元格,但我能弄清楚如何做到这一点的唯一方法是写入以取消保护,然后在单击提交按钮时重新保护工作表。不幸的是,尽我所能,我不完全确定在哪里放置编码,并希望它足够简单,但我尝试的一切都会出现错误。

这是原始代码 - 有什么想法可以在这段代码中放置取消保护、运行代码,然后在同一个 VBA 中重新保护所有代码吗?

Sub Submit_Details()
        Dim shDrug As Worksheet
        Dim shLogging As Worksheet
        Dim iCurrentRow As Integer
        Dim sDrugName As String
    
        Set shLogging = ThisWorkbook.Sheets("Logging")
        sDrugName = shLogging.Range("k7").Value
        Set shDrug = ThisWorkbook.Sheets(sDrugName)
        iCurrentRow = shDrug.Range("A" & Application.Rows.count).End(xlUp).Row + 1

        If MsgBox("Is this a PA?" & vbNewLine & "If a Charging issue, click 'No'", vbYesNo + vbQuestion, confirmation) <> vbNo Then
    
        With shDrug
            .Cells(iCurrentRow, 1) = Format([now()], "DD-MMM-YYYY HH:MM:SS")
            .Cells(iCurrentRow, 2) = shLogging.Range("G9")
            .Cells(iCurrentRow, 3) = shLogging.Range("G10")
            .Cells(iCurrentRow, 4) = shLogging.Range("G11")
            .Cells(iCurrentRow, 5) = shLogging.Range("G12")
            .Cells(iCurrentRow, 6) = shLogging.Range("G13")
            .Cells(iCurrentRow, 8) = shLogging.Range("G14")
            .Cells(iCurrentRow, 10) = shLogging.Range("G15")
            .Cells(iCurrentRow, 11) = shLogging.Range("G16")
            .Cells(iCurrentRow, 12) = shLogging.Range("G18")
            .Cells(iCurrentRow, 17) = Format("Yes")
            .Cells(iCurrentRow, 18) = shLogging.Range("G17")
            .Cells(iCurrentRow, 19) = Format("--")
            .Cells(iCurrentRow, 20) = Format("Pending")
      
        End With
    
        shLogging.Range("G9:G18").Value = ""
    
        MsgBox "Please call Patient/Family about status!" & vbNewLine & "Data submitted successfully!"
    
        ElseIf MsgBox("Did you send back to the Clinic for processing?", vbYesNo, confirmation) <> vbNo Then
        
        With shDrug
            .Cells(iCurrentRow, 1) = Format([now()], "DD-MMM-YYYY HH:MM:SS")
            .Cells(iCurrentRow, 2) = shLogging.Range("G9")
            .Cells(iCurrentRow, 3) = shLogging.Range("G10")
            .Cells(iCurrentRow, 4) = shLogging.Range("G11")
            .Cells(iCurrentRow, 5) = shLogging.Range("G12")
            .Cells(iCurrentRow, 6) = shLogging.Range("G13")
            .Cells(iCurrentRow, 8) = shLogging.Range("G14")
            .Cells(iCurrentRow, 10) = shLogging.Range("G15")
            .Cells(iCurrentRow, 11) = shLogging.Range("G16")
            .Cells(iCurrentRow, 12) = shLogging.Range("G18")
            .Cells(iCurrentRow, 17) = Format("No")
            .Cells(iCurrentRow, 18) = Format("--")
            .Cells(iCurrentRow, 19) = Format("Yes")
            .Cells(iCurrentRow, 20) = Format("--")
         End With
    
        shLogging.Range("G9:G18").Value = ""
    
        MsgBox "Thank you!" & vbNewLine & "Please continue processing PA's"
    
        ElseIf MsgBox("Please send back to the Clinic for processing!", vbOKOnly, confirmation) Then
        
        With shDrug
            .Cells(iCurrentRow, 1) = Format([now()], "DD-MMM-YYYY HH:MM:SS")
            .Cells(iCurrentRow, 2) = shLogging.Range("G9")
            .Cells(iCurrentRow, 3) = shLogging.Range("G10")
            .Cells(iCurrentRow, 4) = shLogging.Range("G11")
            .Cells(iCurrentRow, 5) = shLogging.Range("G12")
            .Cells(iCurrentRow, 6) = shLogging.Range("G13")
            .Cells(iCurrentRow, 8) = shLogging.Range("G14")
            .Cells(iCurrentRow, 10) = shLogging.Range("G15")
            .Cells(iCurrentRow, 11) = shLogging.Range("G16")
            .Cells(iCurrentRow, 12) = shLogging.Range("G18")
            .Cells(iCurrentRow, 17) = Format("No")
            .Cells(iCurrentRow, 18) = Format("--")
            .Cells(iCurrentRow, 19) = Format("No")
            .Cells(iCurrentRow, 20) = Format("--")
        End With
    
        shLogging.Range("G9:G18").Value = ""
    
        MsgBox "Thank you!" & vbNewLine & "Please continue processing PA's"
        
   
    End If

End Sub

放入Worksheet.Unprotect和Worksheet.Protect函数。

excel vba password-protection
1个回答
0
投票

记录一个保护的宏,记录一个不保护的单独宏,然后编写第四个类似 tzo 的子

Sub DoTheThings()
  Call NameOfUnprotectSub()
  On Error Resume Next
  Call Submit_Details()
  Call NameOfReprotectSub()
End Sub

On Error Resume Next
在那里,因此,如果出现问题,无论如何都会尝试再次设置保护。

一旦你开始工作,你就可以尝试让它变得更好......

  • 也许可以用一个带有真/假参数的子例程替换取消保护和重新保护子例程,这样相同的代码就会无限循环并禁用保护。
  • 也许将
    On Error Resume Next
    替换为仍会重新保护的错误处理,但 also 告诉用户存在错误。

但这超出了你的问题范围,即使是丑陋的工作代码也比未完成的优雅代码更好。

© www.soinside.com 2019 - 2024. All rights reserved.