LoginSignup
0
0

More than 1 year has passed since last update.

Excelのセルのスタイルをすべてクリアする。

Posted at

Excelで他のブックからコピーアンドペーストすると、そのセルの独自スタイルが追加されることがあります。
すると、セルのスタイル欄がどんどん増えます。
そして、セルのスタイルが無駄に多いと動きがにぶくなることがあります。

そこで、以下のマクロで既定以外のセルのスタイルを強制消去します。

Sub clearAllStyles()
  On Error Resume Next
  
  Dim objStyle As Style
  
  For Each objStyle In ActiveWorkbook.Styles
    'ビルトインスタイル(削除不可)以外を削除
    If objStyle.BuiltIn = False Then
      objStyle.Delete
      If Err.Number <> 0 Then
        MsgBox (Err.Description & "Locked? " & objStyle.Locked)
        Err.Clear
      End If
    End If
  Next
End Sub

ポイントは「On Error Resume Next」
これがないと削除エラーが発生したところでマクロが中断し、
それ以降のスタイルは消すことができません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0