Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Excel - Too Many Different Cell Formats

November 10, 2017 Marcus Small
Excel too many colours

I was recently sent a file which had the interesting warning message Too many different cell formats.  I had not seen this message before and upon doing some research found it was  common problem.  The message is annoying as after you click OK  it does not allow you to add additional formatting even if that is a used format within the file.  The solution was rather complex however effective, I found some code which looked to do the job and cleaned it up to the point where I was happy with it. 

Stack Overflow Solution
 

The solution is robust and does a nice job.  I have expanded upon it and have a nice concise bit of code.

This is the error message in Excel which stops any style dead.

This is the error message in Excel which stops any style dead.

The following is the code to achieve the task.  It can take a while depending on the sheets you have in the workbook.  You will need to create a link to Excel Scripting Runtime under Tools in the VBA editor.

Too many cell formats VBA

The following will work upon completion of the above.

Option Explicit

Sub CleanFile()
Dim obj As Style
Dim rng As Range
Dim wb As Workbook
Dim sh As Worksheet
Dim str As String
Dim aKey As Variant
Dim i As Long
Dim dict As New Scripting.Dictionary
' Tools / References / Microsoft Scripting Runtime

Set wb = ActiveWorkbook
MsgBox "Start # of styles : " & wb.Styles.Count ' Start sytles in wb
    For Each obj In wb.Styles
      str = obj.NameLocal
      i = i + 1
      Call dict.Add(str, 0)
    Next obj

  For Each sh In Sheets
    If sh.Visible Then
      For Each rng In sh.UsedRange.Cells
        str = rng.Style
        dict.Item(str) = dict.Item(str) + 1
      Next rng
    End If
   Next
sh
On Error Resume Next 'Cover for delete error
    For Each aKey In dict.Keys
      If dict.Item(aKey) = 0 Then
        Call wb.Styles(aKey).Delete 'not used
          If Err.Number <> 0 Then Err.Clear
          Call dict.Remove(aKey)
      End If
   Next
aKey

MsgBox "End # of style : " & wb.Styles.Count ' End sytles in wb
End Sub

Paste into a normal module and it should do the trick.  It may take a while to run depending on how many styles are in your workbook.  Be patient - then love it!!!!

← Make Excel SpeakProject Modelling in Excel →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.