使几个细胞可编辑,并且很少有细胞在excel中受到保护

时间:2014-10-30 05:03:41

标签: excel vba excel-vba

我有一张excel表,其中包含第8行到最后的数据以及从A列到AZ的数据我写了一个宏,当用户选择一个单元格时,它会获取行地址并提示输入起始列名称和结束列名称,因此工作表在这些单元格中变得臃肿。问题是如果我想在最后插入行,它不允许我添加。如何在这种情况下插入一行。

此excel分发给用户,用户应该能够添加行,但不能编辑我锁定的列。这该怎么做? 这是我写的宏:

Sub Row_Locker()
Dim locat As String
Dim colstart As String
Dim colend As String
Dim topath As String
ActiveSheet.Protect Password:="mbt"
ActiveSheet.Unprotect
rlocat = ActiveCell.Row
clocat = ActiveCell.Column
colstart = InputBox("enter the start column name")
colend = InputBox("enter the end column name")
topath = colstart & "8" & ":" & colend & rlocat
Cells.Select
' unlock all the cells
Selection.Locked = False
' next, select the cells (or range) that you want to make read only,
' here I used simply A1
Range(topath).Select
' lock those cells
Selection.Locked = True
' now we need to protect the sheet to restrict access to the cells.
' I protected only the contents you can add whatever you want
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False
End Sub

excel表看起来像这样,所以如果我选择黄色框,如果运行宏,它上面的列内容和它旁边的列内容将被阻止。并在第一个输入框中键入“N”,在第二个输入框中键入“O” enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

ActiveSheet.Protect Password:="your pw", AllowInsertingRows:=True ' , other if needed,

参考:WorkSheet.Protect

expression.Protect(Password,DrawingObjects,Contents,Scenarios,UserInterfaceOnly,AllowFormattingCells,AllowFormattingColumns,AllowFormattingRows,AllowInsertingColumns,AllowInsertingRows,AllowInsertingHyperlinks,AllowDeletingColumns,AllowDeletingRows,AllowSorting,AllowFiltering,AllowUsingPivotTables)

expression:表示Worksheet对象的变量。