2017-07-24 80 views
0

我是VBA的新手。我正在嘗試製作一個按鈕,以清除未受保護的單元格中的值。我有一張受保護的紙張,但我想確保只有未受保護的紙張被清除。我需要單元格中的範圍嗎?在此先感謝按鈕清除不受保護的單元格

回答

1

工作表是保護,單元格是鎖定

試試這個:

Sub KlearStuff() 
    Dim r As Range, rKlear As Range 
    Set rclear = Nothing 
    For Each r In ActiveSheet.UsedRange 
     If r.Locked = False Then 
      If rKlear Is Nothing Then 
       Set rKlear = r 
      Else 
       Set rKlear = Union(rKlear, r) 
      End If 
     End If 
    Next r 
    rKlear.Clear 
End Sub 

編輯#1:
這將清除內容只:

Sub KlearStuff() 
    Dim r As Range, rKlear As Range 
    Set rclear = Nothing 
    For Each r In ActiveSheet.UsedRange 
     If r.Locked = False Then 
      If rKlear Is Nothing Then 
       Set rKlear = r 
      Else 
       Set rKlear = Union(rKlear, r) 
      End If 
     End If 
    Next r 
    rKlear.ClearContents 
End Sub 
+0

這改變了我的格式。我只需要不受保護的單元格就像5的值一樣變成格式相同的空白單元格 – Bsmith

+0

@Bsmith請參閱我的**編輯#1 ** –

相關問題