Excel

Standardize all Cell Formats in a Workbook

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro makes several formatting changes to all cells in all worksheets in a workbook. Avoids the Too Many Cell Formats error. 

Discussion:

You have several worksheets in a workbook and you need to make some formatting changes. This macro illustrates how to loop through every worksheet in the active workbook, domonstrates how to make several formatting changes, and helps to get rid of the Too Many Cell Formats error. 

Code:

instructions for use

			

Option Explicit Sub Formatting() Dim WS As Worksheet Dim RngConstants As Range Dim RngFormulas As Range Dim RngCell As Range For Each WS In Worksheets With WS.Cells 'Remove borders .Borders.LineStyle = xlNone 'Remove color fills .Interior.ColorIndex = xlNone 'Change the Row Height and Column Width .RowHeight = 12.75 .ColumnWidth = 8.43 'Change several Font properties With .Font .ColorIndex = 0 .Bold = False .Italic = False .Underline = False .Name = "Arial" .Size = 10 End With End With With WS 'Change the format of all dates to mm/dd/yy 'Narrow the range we are looping through On Error Resume Next Set RngConstants = .Cells.SpecialCells(xlCellTypeConstants, 1) Set RngFormulas = .Cells.SpecialCells(xlCellTypeFormulas, 1) On Error GoTo 0 'Loop through cells with constants If Not RngConstants Is Nothing Then For Each RngCell In RngConstants If IsDate(RngCell.Value) Then .Range(RngCell.Address).NumberFormat = "mm/dd/yy;@" End If Next End If 'Loop through cells with formulas If Not RngFormulas Is Nothing Then For Each RngCell In RngFormulas If IsDate(RngCell.Value) Then .Range(RngCell.Address).NumberFormat = "mm/dd/yy;@" End If Next End If End With Next End Sub

How to use:

  1. Copy the code above.
  2. Open Excel.
  3. Press Alt+F11 to open the Visual Basic Editor (VBE).
  4. Insert-Module.
  5. Paste the code in the Main Code Window for that module.
  6. Close the VBE (Alt + Q or press the X in the top-right corner).
 

Test the code:

  1. Tools-Macro-Macros and double-click Formatting.
 

Sample File:

Formatting.zip 9.19KB 

Approved by mdmackillop


This entry has been viewed 162 times.

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