Excel

Delete styles from other workbooks in a folder

Ease of Use

Easy

Version tested with

2000, 2003 

Submitted by:

johnske

Description:

After running the code, all the styles (except 'Normal') in all the other workbooks in the folder will be cleared. (Caveat: This will not work in workbooks that have any sort of protection applied)... Note: You can copy the code yourself or download the attachment (which is basically a "tool" for clearing styles) 

Discussion:

Often, when things are copied from workbook to workbook all the styles in the first workbook are incorporated in the new workbook. When this is done many times it can reach a the point where you simply cannot format cells any more. Microsoft's knowledge base contains a procedure that is supposed to clear all these styles, but it completely fails when previous users have used a weird 'name' for a style that can't be recognized by Excel as being the name of a style, and this is common. An example of such an error is for users to use a number format as a 'name' for the style, this will cause MS's procedure to 'crash and burn'. The following procedure will delete almost all of those styles, however, note that there may be a couple of even weirder styles that it can't delete e.g. if a user uses =(a hyperlink or path) for the 'name' of a style. - (But it won't crash when these names are met) 

Code:

instructions for use

			

Option Explicit '<< CLEAR THE STYLES FROM ALL BOOKS IN THIS FOLDER >> Sub ClearStyles() Dim i&, N&, Cell As Range, RangeOfStyles As Range MsgBox "Depending on how many files & styles you have" & vbLf & _ "this may take some time, so please be patient" & vbLf & _ "" & vbLf & _ "(You will be told when the process is finished)", _ vbInformation, "Information..." Application.ScreenUpdating = False Application.EnableEvents = False With Application.FileSearch .LookIn = ActiveWorkbook.Path .Filename = "*.xls" If .Execute > 0 Then For N = 1 To .FoundFiles.Count If .FoundFiles(N) <> ThisWorkbook.FullName Then Application.Workbooks.Open(.FoundFiles(N)).Activate 'Clear Styles '******************************************** 'Add a temporary sheet Sheets.Add before:=Sheets(1) 'List all the styles For i = 1 To ActiveWorkbook.Styles.Count [a65536].End(xlUp).Offset(1, 0) = ActiveWorkbook. _ Styles(i).Name Next Set RangeOfStyles = Range(Columns(1).Rows(2), _ Columns(1).Rows(65536).End(xlUp)) For Each Cell In RangeOfStyles If Not Cell.Text Like "Normal" Then On Error Resume Next ActiveWorkbook.Styles(Cell.Text).Delete ActiveWorkbook.Styles(Cell.NumberFormat).Delete End If Next Cell '******************************************** 'delete the temp sheet Application.DisplayAlerts = False ActiveSheet.Delete ActiveWorkbook.Close savechanges:=True End If Next N End If End With MsgBox "Styles cleared from workbooks", vbInformation, "Completed..." End Sub

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the code into the Module
  5. Now select File/Close and Return To Microsoft Excel
  6. Save your work
 

Test the code:

  1. Select Tools/Macro/Macros.../ClearStyles/Run
  2. After running the code, open one of the other books in the folder...
  3. Go to Format/Styles... and check that the styles have been deleted
  4. NOTE: This is NOT applied to the active workbook, only to the others in the folder
  5. To apply this to the active book, you need to look at the allied knowledge-base entry http://www.vbaexpress.com/kb/getarticle.php?kb_id=367
 

Sample File:

DeleteStyles.zip 14.55KB 

Approved by mdmackillop


This entry has been viewed 67 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express