Sub test()
Dim vUnlocked As Variant
Dim rngUnlocked As Range
With ActiveSheet
.Unprotect Password:="abcd" '<<<<这好象要加入解保护??
If .ProtectContents = True Then .Unprotect
vUnlocked = .UsedRange
.UsedRange.ClearContents
.Protect
On Error Resume Next
.UsedRange.Value = 1
.Unprotect
Set rngUnlocked = .UsedRange.SpecialCells(xlCellTypeConstants)
.UsedRange = vUnlocked
rngUnlocked.Select
.Protect Password:="abcd" '<<<< 这好象要加入加保护???
End With
Selection.ClearContents
End Sub
我师傅给了我个另类的代码,可以试试:
Sub test()
Dim vUnlocked As Variant
Dim rngUnlocked As Range
With ActiveSheet
If .ProtectContents = True Then .Unprotect
vUnlocked = .UsedRange
.UsedRange.ClearContents
.Protect
On Error Resume Next
.UsedRange.Value = 1
.Unprotect
Set rngUnlocked = .UsedRange.SpecialCells(xlCellTypeConstants)
.UsedRange = vUnlocked
rngUnlocked.Select
End With
selection.clearcontents
End Sub