3 minute read

🚀 Introduction: Why Automate Excel Formatting?

When working with Excel files that contain dozens of worksheets—such as monthly reports, budget trackers, or project summaries—it can quickly become overwhelming to manage formatting consistently across all sheets.

Common repetitive tasks include:

  • Renaming worksheets to reflect the reporting period or sheet owner
  • Coloring worksheet tabs for quick identification
  • Setting up uniform headers and footers
  • Formatting sheets for printing or PDF export

Doing these manually across multiple worksheets is tedious and prone to inconsistency or errors.

That’s where VBA (Visual Basic for Applications) comes in. With just a few lines of code, we can automate all these tasks in seconds. In this tutorial, we’ll walk through a macro called UpdateWorksheetMetadata that brings consistency, clarity, and a professional look to our Excel files—without the manual grunt work.

🔧 What This VBA Macro Does

The macro handles four main tasks across every worksheet:

1. Automatically Renames Each Worksheet

Each sheet is renamed using data from:

  • B2: Month (e.g., “June”)
  • F1: Year (e.g., “2025”)
  • F5: Owner (e.g., “Rakesh”)

Example: June (Rakesh), 2025

2. Applies Color to Worksheet Tabs

Different tab colors are applied based on the sheet owner (F5), helping us visually identify who’s responsible.

3. Standardizes Headers and Footers

Each sheet includes:

  • The sheet name in the center header
  • A label like “Calculation of June 2025” in the footer
  • Dynamic page numbers

4. Applies Uniform Page Setup Formatting

Basic formatting like headers, footers, and alignment are standardized across sheets.

🧠 Why This Matters

This automation:

  • Saves Time: No need to rename or format sheets manually.
  • Improves Accuracy: Reduces typos or missed sheets.
  • Ensures Consistency: Every sheet follows the same structure.
  • Makes Navigation Easier: Colors and naming help us find what we need faster.
  • Creates a Professional Appearance: Uniform formatting reflects attention to detail.

📜 Full VBA Code Explained Step-by-Step

Paste this code into a new module in the VBA editor:

Sub UpdateWorksheetMetadata()
    Dim month As String
    Dim year As String
    Dim owner As String
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        month = Trim(ws.Range("B2").Value)
        year = Trim(ws.Range("F1").Value)
        owner = Trim(ws.Range("F5").Value)

        If month = "" Or year = "" Or owner = "" Then
            MsgBox "Missing data in sheet: " & ws.Name & vbCrLf & "Ensure B2 (Month), F1 (Year), and F5 (Owner) are filled.", vbExclamation
            GoTo NextSheet
        End If

        Dim newName As String
        newName = month & " (" & owner & "), " & year

        If ws.Name <> newName Then
            On Error Resume Next
            ws.Name = newName
            If Err.Number <> 0 Then
                MsgBox "Could not rename sheet: " & ws.Name & vbCrLf & "Sheet name might already exist.", vbCritical
                Err.Clear
            End If
            On Error GoTo 0
        End If

        Select Case LCase(owner)
            Case "rakesh"
                ws.Tab.Color = RGB(33, 92, 152)
            Case "anita"
                ws.Tab.Color = RGB(255, 192, 0)
            Case "james"
                ws.Tab.Color = RGB(112, 48, 160)
            Case Else
                ws.Tab.Color = RGB(0, 176, 80)
        End Select

        With ws.PageSetup
            .LeftHeader = ""
            .CenterHeader = "&""Arial,Bold""&14" & newName
            .RightHeader = ""
            .LeftFooter = "Calculation of " & month & " " & year
            .CenterFooter = ""
            .RightFooter = "Page &P of &N"
        End With
NextSheet:
    Next ws
End Sub

🛠️ How to Install and Run This Macro

  1. Open our Excel workbook.
  2. Press ALT + F11 to open the VBA Editor.
  3. Go to Insert > Module.
  4. Paste the code above.
  5. Close the editor.
  6. In Excel, go to the Developer tab.
  7. Click “Macros”, select UpdateWorksheetMetadata, and click Run.

Before running: Make sure each sheet has:

  • B2: Month
  • F1: Year
  • F5: Owner

The macro skips any sheet missing this data.

🔄 Ideas for Enhancements

  • 🔔 Add a summary log of renamed sheets
  • 🎨 Add more color mappings for owners
  • ⚙️ Use named ranges or a config sheet
  • 📁 Export sheets to PDF after formatting

✅ Final Thoughts

Automation in Excel helps us work smarter. With this macro, we bring clarity, uniformity, and professionalism to our spreadsheets—saving time and mental energy.

Whether we’re preparing reports, managing projects, or collaborating with teams, automating repetitive formatting improves both speed and quality.

Leave a comment