Finance

Charts

Statistics

Macros

Search

Formatting a Worksheet With Excel VBA

The following VBA procedure demonstrates how to apply font formatting to all cells of a specific worksheet:

Sub FormatSheet()
    ThisWorkbook.Activate
    Worksheets("Sheet3").Cells.Font.Name = "Arial"
    Worksheets("Sheet3").Cells.Font.Size = 10
End Sub

Explanation:

  1. Activate the current workbook
  1. Activate

Ensures that all operations are performed within the workbook where the macro resides.

Access and format all cells on a worksheet

Worksheets(« Sheet3 »).Cells.Font.Name = « Arial »

Worksheets(« Sheet3 »).Cells.Font.Size = 10

    • Worksheets(« Sheet3 ») refers to the specific worksheet to be formatted.
    • .Cells refers to all cells in that sheet (the entire grid).
    • .Font accesses the font formatting settings of those cells.
    • .Name = « Arial » sets the font to Arial.
    • .Size = 10 sets the font size to 10.

Understanding the Object Hierarchy:

This example shows a clear VBA object hierarchy:

Worksheet ➝ Cells ➝ Font ➝ Name / Size

Each level refines what part of the worksheet you’re addressing:

  • Cells accesses all the cells.
  • Font accesses their font formatting.
  • Name and Size are properties that define font appearance.

What Else Can Be Formatted with .Font?

The .Font object includes many other customizable properties:

  • .Bold = True — makes text bold
  • .Italic = True — applies italics
  • .Color = RGB(255, 0, 0) — sets font color (in this case, red)
  • .Underline = xlUnderlineStyleSingle — adds underline

Example:

With Worksheets("Sheet3").Cells.Font
    .Bold = True
    .Color = RGB(0, 0, 255)
End With
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx