Question

  1. I have a massive file with 10 sheets recreated from scratch, 12 sheets updated, 5 sheets loaded with raw data, and 7 sheets that are used by the macros for the report.
  2. I have recently added a new sheet and am running into the Excel "Too many different cell formats" problem.

what I have tried

I have read some of the google search results and they say I should simplify the formatting, but I don't even know how I got up to 4000 distinct cell formats, let alone how much I'm using which ones, so I can remove some.

It is also popping up some times the file is run, but not all, until such time as it comes up, and then it happens every time it is run. Since the macro is doing so much of the work, including creating the 10 sheets from scratch, I am not sure what to do.

Does anyone know

  • of a Macro I could run to get a listing of all the cell formats and how many cells are using them?
  • of a program they trust to help remove excess cell formats?

Thanks

Was it helpful?

Solution

"Cell formats" are complicated. Cells do not really have a "format". They have a font (which itself has a name and a size), a NumberFormat, Height, Width, Orientation, etc.

So you need to define what you mean by "format".

Below is code to get the Font Name and Size. You can substitute whatever attributes you like.

The code below assumes you have created a Worksheet named "Formats" in the workbook. After you run the macro, the Font Names and sizes will be listed in that worksheet.

Public Sub GetFormats()

    Dim CurrentSheet As Integer
    Dim UsedRange As Range
    Dim CurrentCell As Range
    Dim rw As Long

    Sheets("Formats").Cells.ClearContents
    rw = 1
    For CurrentSheet = 1 To Sheets.Count
        Set UsedRange = Range(Sheets(CurrentSheet).Range("A1"), Sheets(CurrentSheet).Range("A1").SpecialCells(xlLastCell))
        For Each CurrentCell In UsedRange
            FontUsed = CurrentCell.Font.Name + ":" + CStr(CurrentCell.Font.Size)
            If Sheets("Formats").Cells.Find(FontUsed) Is Nothing Then
                Sheets("Formats").Cells(rw, 1).Value = FontUsed
                rw = rw + 1
            End If
        Next
    Next CurrentSheet
End Sub

OTHER TIPS

The problem you describe caused me (and a coworker) to lose many hours of productivity when using Excel 2010. The following VBA code / macro helped me to drop a .xlsm file from using 3540 styles down to 34.

' Description:
'    Borrowed largely from http://www.jkp-ads.com/Articles/styles06.asp

Option Explicit

' Description:
'    This is the "driver" for the entire module.
Public Sub DropUnusedStyles()

    Dim styleObj As Style
    Dim rngCell As Range
    Dim wb As Workbook
    Dim wsh As Worksheet
    Dim str As String
    Dim iStyleCount As Long
    Dim dict As New Scripting.Dictionary    ' <- from Tools / References... / "Microsoft Scripting Runtime"

    ' wb := workbook of interest.  Choose one of the following
    ' Set wb = ThisWorkbook ' choose this module's workbook
    Set wb = ActiveWorkbook ' the active workbook in excel


    Debug.Print "BEGINNING # of styles in workbook: " & wb.Styles.Count
    MsgBox "BEGINNING # of styles in workbook: " & wb.Styles.Count

    ' dict := list of styles
    For Each styleObj In wb.Styles
        str = styleObj.NameLocal
        iStyleCount = iStyleCount + 1
        Call dict.Add(str, 0)    ' First time:  adds keys
    Next styleObj
    Debug.Print "  dictionary now has " & dict.Count & " entries."
    ' Status, dictionary has styles (key) which are known to workbook


    ' Traverse each visible worksheet and increment count each style occurrence
    For Each wsh In wb.Worksheets
        If wsh.Visible Then
            For Each rngCell In wsh.UsedRange.Cells
                str = rngCell.Style
                dict.Item(str) = dict.Item(str) + 1     ' This time:  counts occurrences
            Next rngCell
        End If
    Next wsh
    ' Status, dictionary styles (key) has cell occurrence count (item)


    ' Try to delete unused styles
    Dim aKey As Variant
    On Error Resume Next    ' wb.Styles(aKey).Delete may throw error

    For Each aKey In dict.Keys

        ' display count & stylename
        '    e.g. "24   Normal"
        Debug.Print dict.Item(aKey) & vbTab & aKey

        If dict.Item(aKey) = 0 Then
            ' Occurrence count (Item) indicates this style is not used
            Call wb.Styles(aKey).Delete
            If Err.Number <> 0 Then
                Debug.Print vbTab & "^-- failed to delete"
                Err.Clear
            End If
            Call dict.Remove(aKey)
        End If

    Next aKey

    Debug.Print "ENDING # of style in workbook: " & wb.Styles.Count
    MsgBox "ENDING # of style in workbook: " & wb.Styles.Count

End Sub

Lots of people seem to run into this problem.

Most often the issue is related to the excessive number of unused and often corrupted styles and not so much the total count of the cell unique cell format combos.

I wrote a utility to fix XL2007 OOXML files that can be saved down to XL2003. Here is the link to the blog post:

  • Requires .Net3.5 and MS Excel 2007.
  • Will fix xlsx or xlsm files.
  • The post has a ReadMe file to go with the app.

No need to run the risk of further corrupting your file by using Open Office like it is suggested on some other forums

The bugs that lead to "Too many cell formats" error message in Excel 2007+ have been addressed: http://sergeig888.wordpress.com/2011/05/06/msft-released-hot-fix-for-excel-2007-custom-styles-duplication/ Note that the bug fixes will not remove pre-existing styles related file corruption. Open XML based tools (available for free) are the only option that can remove elements inaccessible to the Excel object model based tools: e.g., bad styles that disguised themselves as built-in, hidden styles, etc... Open XML based cleanup means 100% styles related corruption free files.

I had this problem, found the easiest way to clear it was using this Excel add in. It appears to be the "official" answer from the Microsoft page on the problem.

For people that are as confused as I was at using .xlam files, after downloading it you do this in Excel:

  1. Click on File < Options < Add-ins.
  2. Under Manage option click on Go.
  3. In the Add-ins window click on Browse, browse to the location where the XLAM file is saved, highlight it and click Ok.
  4. Enable the new add-in in the Add-ins window and click Ok.
  5. On your home screen ribbon bar there should now be a "Remove Styles" section, which consists of a button showing the number of styles in the workbook (probably thousands if you're having this problem).
  6. Click the button on the ribbon and it will eliminate all the duplicate formats.

If you don't see it, check that it's showing on the ribbon correctly. Right click on the ribbon and say "Customize Ribbon". Then click on Add-Ins. Under Active Application Add-ins you should see "Remove Extra Styles".

2019 Update: At that link they have now put everything in a big zip file with discussions about the issue and so on. In that zip file there is another zipped folder called source code, in that folder you can find the .xlam file.

SpreadsheetGear for .NET will coallesce non-unique formats which may help with your situation.

You can download the free trial here if you want to see whether it helps. Just load the workbook into the "SpreadsheetGear 2009 for Windows" application which is installed with the evaluation software and then save the workbook.

If you actually have that many unique formats, you will have to simplify. Every unique combination of font / cell color (Interior), number format, horizontal and vertical alignment, borders, indent level, and probably a few things I'm not thinking of will cause a unique entry in the table.

Another option is to switch to Excel 2007 which has the limit on unique cell formats increased from 4,000 to 64K.

Disclaimer: I own SpreadsheetGear LLC

I have seen this problem before. It is possible to recreate to prove what I am abaout to say. It is a little bit of a catch 22 but when you have a sheet that has the 'Too many formats' problem, open a brand new sheet and copy one cell from the too many formats sheet and simply paste it into the new workbook. This workbook will now be 'Infected' as it were and will also have the too many formats error. It seems that a lot of the format data comes over with that paste unless you use a paste special to restrict it.

How to make things better? Well a workaround is to use the Save As HTML function. Bear with me though this does work. I am talking about 2003 here, I don't know what happens in 2007, maybe this bug has been fixed. So ... Save as Html then close excel. Load a new session and load this in making sure you don't run any macros and then once loaded save as a spreadsheet.

When you close and reopen this new spreadsheet your Too Many Formats woes should be gone.

One solution may be to use ASAP utilities. In the sheet section there is a remove all unused styles option. You then have to close the workbook and re-open, I think.

This will delete all the styles EXCEPT for the default styles (normal, Explanatory, 20% Accent1 etc). Including styles that the user has created but it's a quick and dirty way to clean up a workbook:


Sub NewNukeStyles()
Dim tempstyle As Style

For Each tempstyle In ActiveWorkbook.Styles

 If tempstyle.BuiltIn = False Then
    If tempstyle.Locked = True Then 'not sure what this is
       tempstyle.Delete
    End If
End If

Next tempstyle

End Sub 'NukeStyles

I'd love to know what the tempstyle.locked property actually refers to other than "a Boolean value that indicates if the object is locked".

If you can open the file Try > edit > clear > Formats.

Highlight the sheet before hitting formats it worked for me.

make sure you make a copy of the EXCEL before doing this just incase something is missing you can cross ref against the old document.

It's a bug in Excel that plagues all released versions.

The problem can be fixed using this tool.

Works with XLS and XLSX files.

i pretty much used all the answers here, but the error message stayed.

after reading this microsoft support page (https://support.microsoft.com/en-us/kb/213904), i came to XLStylesTool.exe .

But, again no luck (because i used it wrongly).

I have windows seven, so the windows 8/10 version is a no go.

I read the comments from the download page (because it's not written in the download infos) here https://sergeig888.wordpress.com/2011/03/21/net4-0-version-of-the-xlstylestool-is-now-available/

i found out that i need the .NET4.0 version for making it work under win7 (those dummies can't simply call it win7 version ???)

download link: https://skydrive.live.com/redir?resid=53E1D37F76F69444!900&authkey=!AH5oeGVaWlMsFHA&ithint=file%2c.zip

My next problem was that it does NOT work on *.XLS , so i opened excel and saved as *.XLSM , wich works.

After that just click 'Get Excel File', and 'Process File' (i checked 'commit changes and open in excel', and in excel 'save as' '*.xls'. Close excel, reopen file, WORKS (without anoying warning) !!!

If it gives you an error "too many cell formats" then save the document right away. Change the name of the document to some other name for the time being. Open the document and then save it with the same name as you wanted it earlier. Now close the document and open it, it would not display that error anymore. Sometimes its just gone away with a simple save as well but its kinda weird to me!

One simple solution in case it's a small file:

Just copy all and paste it into Word. It will detect the content as tables. Then select all columns (NOT the complete table via the cross sign in the top left corner) and past it into a new .xls sheet and you will have the clear values only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top