vba 受保护的工作表允许通过复制和粘贴来编辑单元格格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21856681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protected worksheet allows editing cell format by copy and paste
提问by Marco
I have a worksheet which is protected. Only some cells are editable and the user can write into them but cannot change the cell format as usual. If he decides to copy and paste data from another worksheet to mine then the cell formatting of the other worksheet is applied to my cells. I want my cells to be editable in value but their cell format must not be editable at all! How can I do that?
我有一个受保护的工作表。只有一些单元格是可编辑的,用户可以写入其中,但不能像往常一样更改单元格格式。如果他决定将另一个工作表中的数据复制并粘贴到我的工作表中,那么另一个工作表的单元格格式将应用于我的单元格。我希望我的单元格的值可编辑,但它们的单元格格式根本不可编辑!我怎样才能做到这一点?
Thanks in advance!
提前致谢!
Marco
马可
采纳答案by Marco
I used this in order to only paste the values if the user decides to copy and paste in the cells whose format is protected:
我使用它是为了仅在用户决定复制和粘贴格式受保护的单元格时粘贴值:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Application.CutCopyMode = xlCopy Then
Application.EnableEvents = False
Application.Undo
Target.PasteSpecial Paste:=xlPasteValues
Application.EnableEvents = True
End If
End Sub
It undoes any pastes into the worksheet and pastes it again (only values, no formatting).
它撤消任何粘贴到工作表中并再次粘贴(仅值,无格式)。
回答by Pedrumj
One method would be using the worksheet_change event to see if any of the cells have changed:
一种方法是使用 worksheet_change 事件来查看是否有任何单元格已更改:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("J2").Address Then
'your code
End If
End Sub
Next apply the original formatting to the cells that have changed.
接下来将原始格式应用于已更改的单元格。