[Tags: guide, excel, accounting]
OPTION 1: The macro below will show a prompt "Delete Style ____" for each style which and you will need to click "YES" for each style. If you are not concerned about deleting a valid style you can use OPTION 2
Sub DeleteStyles()
'
' DeleteStyles Macro
'
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
End If
Next styT
End Sub
OPTION 2:The macro below will delete all unused styles without a prompt. This is faster but can be more dangerous than OPTION 1. Make sure you save the file both before and after running the macro (thanks to Nick)
Sub DeleteStyles()
'
' DeleteStyles Macro
'
Dim styT As Style
Dim intRet As Integer
For Each styT In ActiveWorkbook.Styles
If Not styT.BuiltIn Then
styT.Delete
End If
Next styT
End Sub
OPTION 3: For those that do not want to deal with Macro's there is a tool you can download that I also find very helpful and easy to use. The tool removes unused cell styles, "stubborn styles" that you can't delete through Excel UI, bad named ranges, named ranges with external references, and unhides non-system created hidden named ranges. Download XLCleaner here: https://skydrive.live.com/?cid=53e1d37f76f69444&id=53E1D37F76F69444!526&sc=documents
- Related Articles:
- Audit Shortcuts for Excel
- Bank of England provides best historical foreign exchange rates data
- Free Microsoft Excel Master Help Provided
Reader Comments (Page 2 of 2)
Thursday July 25, 2013 @ 12:42 AM
Thx
Sub DeleteStyles()
'
' DeleteStyles Macro
'
Dim styT As Style
Dim intRet As Integer
For Each styT In ActiveWorkbook.Styles
If Not styT.BuiltIn Then
styT.Delete
End If
Next styT
End Sub
Modified this way and no need to press the Enter keys :)
Thanks Again for the code
Tuesday October 15, 2013 @ 03:10 PM
The XLStyles tool worked great
Sunday April 13, 2014 @ 04:26 AM
TJ,
I have slightly different problem in that I have a spreadsheet which was beautifully formatted using only a few custom stlyes I had created. I then pasted in some data from another spreadsheet and a load of junk cell styles have been added as well.
Examples of these cell styles are:
ÅëÈ [0]_´ë¿ìÃâÇÏ¿äû
and
386grabber=VGA.3GR (this one is particularly weird as it is preceded and followed by a number of spaces and a line break)
I am unable to delete these using the Excel GUI... righ-click > delete doesn't make them disappear.
Running your message box code in order to try to selectively delete them produces the following error message:
Run-time error '1004':
Delete method of Style class failed
Even running this more vicious code (which deletes everything except the default 'Normal' style) won't get rid of them
Sub StyleKill()
'Removes all styles except for Normal
Dim st As Style
Dim x As Integer
x = 0
On Error Resume Next
For Each st In ActiveWorkbook.Styles
st.Locked = False
st.Delete
x = x + 1
Next
MsgBox ("Deleted " & x & " styles")
End Sub
Do you have any suggestions? I would ideally like to be able to select which styles to delete so that I can retain the custom styles I do want to use.
Thursday May 8, 2014 @ 12:55 PM
Option 3 solved my pbm
Thursday November 5, 2015 @ 10:10 PM
I am trying Option 2 and it is taking time like eons.
"Not Responding" - displayed with the excel file name.
Thursday November 5, 2015 @ 11:51 PM
Guys, I discovered a pretty fast and easy method to get rid of the obnoxious styles (too many formats !!).
Just select the whole worksheet (left hand top button in excel worksheet), and press the "Normal" tab on the Styles ribbon. Repeat this for each of the worksheets.
It worked for me. The macro at Option2 was taking lots of time and didn't work. The above suggestion was a "fluke" kind of thing.
Enjoy!!!
Friday November 6, 2015 @ 02:09 PM
RE: Raghu
The methods in my original posting were to get rid of unused styles. Your suggestion might help solve the "too many styles error" but in doing so it will also remove all styles currently being used and which might cause the issue to reappear down the road, perhaps as you restyle your worksheet, since unused styles still exist. Option 3 tends to work the best for those having issues with Macros.
Tuesday July 26, 2016 @ 04:12 PM
Option 2 worked nicely, thanks
Monday September 5, 2016 @ 04:46 AM
I'm getting a runtime error, highlighting the code: styT.Delete
Friday July 5, 2013 @ 12:05 AM
Thanks so much for the macro Nick. Saved me so much time after failing for over 30 min pressing enter!