How to Lock the data in a cell in excel using vba

后端 未结 5 784
鱼传尺愫
鱼传尺愫 2020-11-27 19:34

I want to stop others from editing the cell contents in my excel sheet using VBA. Is it possible to do this?

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 19:53

    Try using the Worksheet.Protect method, like so:

    Sub ProtectActiveSheet()
        Dim ws As Worksheet
        Set ws = ActiveSheet
        ws.Protect DrawingObjects:=True, Contents:=True, _
            Scenarios:=True, Password="SamplePassword"
    End Sub
    

    You should, however, be concerned about including the password in your VBA code. You don't necessarily need a password if you're only trying to put up a simple barrier that keeps a user from making small mistakes like deleting formulas, etc.

    Also, if you want to see how to do certain things in VBA in Excel, try recording a Macro and looking at the code it generates. That's a good way to get started in VBA.

提交回复
热议问题