简体   繁体   English

Excel工作表保护停止宏

[英]Excel Sheet Protection Stopping Macro

I have been using a piece of code that zooms to fit the the worksheet to the active window. 我一直在使用一段代码来缩放以使工作表适合活动窗口。 However, if I add sheet protection the macro will fail. 但是,如果添加工作表保护,则宏将失败。 To get around this problem, I call a sub to remove the protection then reapply the protection afterwards. 为了解决此问题,我致电了一个子公司以删除保护,然后再重新应用保护。 Is there a way to run my sub without removing and reapplying sheet protection? 有没有一种方法可以运行我的潜水艇而无需删除并重新应用工作表保护?

Code to change zoom: 更改缩放的代码:

Sub fixScreenSize()
    Dim rngX As Range
    Dim myLastCell As Range
    Dim myLastCellAdd As String
    Dim rngToZoom As Range      

        'Get address of last cell containing data
        Set myLastCell = Cells(1, 1).SpecialCells(xlLastCell)
            myLastCellAdd = Cells(myLastCell.Row, myLastCell.Column).Address
        'Find keyword in used range
        Set rngX = Range("A1", myLastCellAdd).Find("myZoomRange", lookat:=xlPart)
        'Zoom to correct range
        Range(Cells(1, 1), rngX).Select
        ActiveWindow.Zoom = True
        Range("A1").Select

    Set myLastCell = Nothing
    Set rngX = Nothing
End Sub
  • Remove all sheet protection 删除所有工作表保护
  • Open the VBE ( Alt + F11 ) 打开VBE( Alt + F11
  • Go to the immediate window ( Ctrl + G ) 转到立即窗口( Ctrl + G
  • Type ActiveSheet.Protect Password:="myPassword", UserInterfaceOnly:=True and press Enter 键入ActiveSheet.Protect Password:="myPassword", UserInterfaceOnly:=True ,然后按Enter

The active worksheet is now protected with the password "myPassword", however the UserInterfaceOnly argument means that your code is still allowed to run on that sheet unaffected by the sheet's protection 现在,活动工作表受到密码“ myPassword”的保护,但是UserInterfaceOnly参数意味着仍允许您的代码在该工作表上运行,不受工作表保护的影响

There is no way to do this that I know of without removing and re-adding the protection. 如果不删除并重新添加保护,就无法做到这一点。 Once the protection is set there is no way to get around a sheet protection without removing the protection. 一旦设置了保护,就无法在不移除保护的情况下绕过纸张保护。

Your way is the correct way and should not impact on performance or anything. 您的方式是正确的方式,不应影响性能或其他任何方面。

EDIT as it turns out there is a way. 事实证明,有一种方法可以编辑。 However I have always done it this way without problems. 但是,我一直以这种方式做到这一点而没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM