Deleting unused Excel custom styles in bulk. How?

14,182

I used the actual name of the error and managed to find this post in StackOverflow:Remove the unknown format in Cell styles

The additional line mpStyle.Locked = FALSE solved my issue.

The full code is now:

Sub ClrStyles()
 Dim mpStyle As Style
  For Each mpStyle In ActiveWorkbook.Styles
   If Not mpStyle.BuiltIn Then
    mpStyle.Locked = FALSE
    mpStyle.Delete
   End If
  Next mpStyle
End Sub

Hope this helps others. Mike.

Share:
14,182

Related videos on Youtube

RocketGoal
Author by

RocketGoal

Updated on September 18, 2022

Comments

  • RocketGoal
    RocketGoal over 1 year

    I've inherited someone's Excel workbook with multiple worksheets, and have discovered that the problems I'm encountering are due to an abundance of custom styles.

    This Microsoft hep file macro counted over 5000+ styles

    Sub styles()
    For i = 1 To ActiveWorkbook.styles.Count
        Worksheets(1).Cells(i, 1) = ActiveWorkbook.styles(i).Name
    Next
    End Sub
    

    I'm trying to remove all the custom (strange) styles, but keep the default/standard styles.

    I've searched for how to do a bulk remove, and keep finding a similar style of macro in the forums. I've chosen the one below but unfortunately the line mpStyle.Delete causes an error:

    Sub ClrStyles()
     Dim mpStyle As Style
      For Each mpStyle In ActiveWorkbook.Styles
       If Not mpStyle.BuiltIn Then
        mpStyle.Delete
       End If
      Next mpStyle
    End Sub
    

    The error it returns is: Delete method of style class failed.

    I'm working in Excel 2010, and the Style class does have a delete function, so I'm confused why it doesn't work.

    Any ideas?