Deleting Unwanted Styles
Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Deleting Unwanted Styles.
When you work with other people who use Excel, it is not unusual to copy worksheets from their workbooks into your own workbook. When you do so, the worksheet isn't the only thing that is copied—Excel also copies their formatting styles to your workbook. Manually deleting the unwanted styles can be a hassle, depending on the number of styles. Removing user-defined styles is very easy, though, if you use a macro. The following macro will quickly delete the unwanted styles:
Dim styT As Style
Dim intRet As Integer
For Each styT In ActiveWorkbook.Styles
If Not styT.BuiltIn Then
intRet = MsgBox("Delete style '" & styT.Name & "'?", vbYesNo)
If intRet = vbYes Then styT.Delete
The macro needs just a little user input. Whenever the macro detects a user-defined style, you are asked if you want to delete it. Clicking on the Yes button causes the style to be removed from the workbook.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2135) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Deleting Unwanted Styles.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Leave your own comment:
Comments for this tip:
Chris 21 Jan 2016, 12:23
Is there a way to do this without the MsgBox? Very annoying when there are dozens of unused styles.
BoasWhip 07 Oct 2015, 05:34
I had a sheet that somehow inherited styles from spreadsheets going back to 1993 (i'm not joking). My own attempt to code was not picking up many of them ... I had used a For/Each Loop.
I ran the macro in edited form and styles appeared top still be there. So I restarted Excel and re-opened the spreadsheet. Between my code and this I was left with the just two native Excel styles. I could not be happier!
Brad Butler 17 Sep 2015, 13:25
Is there a way to permanently delete the default styles for a given workbook? I only want to show about a dozen custom styles. I manually delete the default styles which works for a given Excel session, but they all return the next time that I open the same file.
Ian Clements 16 Apr 2015, 11:54
I've installed and run this macro a few times now in Excel 2013, and nothing happens.
I'm assuming that means my styles are OK
KBLA 23 Sep 2014, 13:17
This code makes excel crash each time. Thanks.
Kenneth Andersen 25 Jun 2014, 07:51
I think it will be very usefull for me, but my problem is, that I have to run the macro over and over again. I expect that the macro each time cleans up a lot but not all. I have no clue how many userdefined styles the workbook has, but it certantly gets faster. The matter is, that I guess I have run the macro 50 times, and I'm not done. I don't know if it is just about to be finished, or I have to run the macro for days. Could the macro make a total cleanup, or maybee tell me how many userdefined styles still left.
Thanks in advance.
Bob Jordan 29 Oct 2013, 18:44
This problem also occurs on Mac Excel 2011 but the styles tool does not work there.
Also the ActiveWorkBook.Styles.Count does not reflect the unused styles on that platform. My ActiveWorkBook.Styles.Count was 27 yet i had 64956 styles in the styles.xml file. I had to resort to a vba cleanup.
I found the XLGeek Coder comments unhelpful in this case.
One needs to get ones code running again by whatever means possible. If there are no Microsoft solutions then one must help ones self.
Michelle Sharp 11 Oct 2013, 16:15
Thanks! I've been using this now for a week on the spreadsheets at work. They were all full of extra styles and were giving us all sorts of errors!!
Has anyone found a way to prevent the styles from accumulating when copying and pasting data between spreadsheets in the first place?
Harish Mali 28 Jan 2013, 07:57
Thank you very much. This macro was really useful and helped me fix the file that I messed up. :)
XLGeek Coder 08 Nov 2012, 17:29
Please don't encourage the use of VBA to deal with styles explosion in Excel workbooks. Excel Object Model based tools (VBA goes against the OM exposed API)can't clean out corrupted system styles or hidden styles. Updated MS KB article on this issue has links to the tools that will guarantee clean up 100%: http://support.microsoft.com/kb/213904
JIm Palmer 07 Nov 2012, 16:29
I had never encountered this problem before. Someone had spent many hours working on a budget file and she was suddenly presented with the message "there are too many styles" after she had copied some data from someone else's file.
Your macro did the trick.
I ended up commenting out the section where it asked the user if they wanted to delete the style.
At first I thought the system had looked up then I broke out of the macro and in the immediate window I entered "? ActiveWorkbook.Sytles.Count"
This showed the number of styles decreasing after running the macro for aobut a minute. It went from over 64,000 to 28,400 and eventually down to about 45 after running the macro a few times.