简体   繁体   中英

How to Lock the data in a cell containing formulas so that they cannot be edited in excel using vba

I am in a very strange situation in VBA. I am new to VBA and struggling since a couple of days in solving this problem.

I want to Lock the top three rows and the first three columns of my excel sheet for editing. However, these cells contain formulas, which I don't want the end user to edit and make changes. So basically, what I need is, the cells should perform all the operations, but should not be edited. How can, I do it in VBA. I am using Excel 2010.

One more thing, I have a button in the top rows ($B$2:$C$3) occupying four cells. This button is assigned to a macro which does the job of clearing contents in all the cells except the cell I Intend to lock. In short, in the other cells, the user can export data from a notepad and thereafter the formulas in the locked cell will do the job. When the user has to export new set of datas, he will cick on the button which is in ($B$2:$C$3) , which clears the existing data and the user can imprt new set of dat from the notepad.

When I use

Worksheets("Sheet1").Range("B2:C3").Locked = False

Worksheets("Sheet1").Protect UserInterfaceOnly:=True 

my button in ($B$2:$C$3) is not working properly. Any help from anybody will behighly solicited. Thanks a lot in advance.

try this:

Worksheets("Sheet1").Range("B2:C3").Locked = true

Worksheets("Sheet1").Protect UserInterfaceOnly:=True 

This should fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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