Excel VBA Tutor Home Excel VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us
Excel VBA Tutor Logo

Lesson 8: Mastering Formatting Functions in Excel VBA


Formatting functions in Excel VBA allow you to present data in a professional and readable manner. This lesson covers both predefined and custom formatting options to enhance your Excel applications.

8.1 Predefined Format Functions

The predefined Format function provides standard formatting options that are commonly used in business applications. The syntax is:

Format(expression, "style_argument")

Pro Tip: Predefined formats are locale-aware, meaning they'll automatically adapt to the regional settings of the user's computer.

Table 8.1: Predefined Format Style Arguments
Style Argument Description Example
General Number Displays numbers without thousands separators Format(8972.234, "General Number") → 8972.234
Fixed Rounds to 2 decimal places without thousands separators Format(8972.234, "Fixed") → 8972.23
Standard Includes thousands separators and rounds to 2 decimal places Format(6648972.265, "Standard") → 6,648,972.27
Currency Adds currency symbol, thousands separators, and rounds to 2 decimals Format(6648972.265, "Currency") → $6,648,972.27
Percent Converts to percentage with % sign and 2 decimal places Format(0.56324, "Percent") → 56.32%
Scientific Displays in scientific notation Format(123456, "Scientific") → 1.23E+05

Practical Example 8.1

Sub FormatSalesData()
    Dim sales As Double
    sales = 1254873.5689
    
    ' Apply different formats to the same number
    Range("A1") = "General Number: " & Format(sales, "General Number")
    Range("A2") = "Fixed: " & Format(sales, "Fixed")
    Range("A3") = "Standard: " & Format(sales, "Standard")
    Range("A4") = "Currency: " & Format(sales, "Currency")
    Range("A5") = "Percent (as decimal): " & Format(sales/10000000, "Percent")
End Sub
Excel VBA Format Function Output Example
Figure 8.1: Output of different format functions in Excel

8.2 User-Defined Format Functions

When predefined formats don't meet your needs, you can create custom formats using special symbols:

Format(expression, "custom_format")

Key Format Symbols:
0 - Digit placeholder (displays zero if no digit)
# - Digit placeholder (displays nothing if no digit)
. - Decimal point
, - Thousands separator
% - Percentage placeholder (multiplies by 100)
$ - Currency symbol

Table 8.2: Custom Format Examples
Example Description Output
Format(781234.57,"0") Rounds to whole number 781235
Format(781234.57,"0.0") Rounds to 1 decimal place 781234.6
Format(781234.576,"0.00") Rounds to 2 decimal places 781234.58
Format(781234.576,"#,##0.00") Adds thousands separators 781,234.58
Format(781234.576,"$#,##0.00") Adds currency symbol $781,234.58
Format(0.576,"0%") Percentage without decimals 58%
Format(0.5768,"0.00%") Percentage with 2 decimals 57.68%
Format(1234.5,"##,##0.00 ""units""") Adds text after number 1,234.50 units

Practical Example 8.2

Private Sub FormatFinancialReport()
    ' Format financial data in various ways
    Cells(1, 1).Value = "Revenue: " & Format(1254873.5689, "$#,##0.00")
    Cells(2, 1).Value = "Growth Rate: " & Format(0.15783, "0.00%")
    Cells(3, 1).Value = "Units Sold: " & Format(12548, "#,##0")
    Cells(4, 1).Value = "Avg. Price: " & Format(99.956, "$#,##0.00")
    Cells(5, 1).Value = "Market Share: " & Format(0.2256, "0.0%")
    
    ' Conditional formatting based on value
    Dim profitMargin As Double
    profitMargin = 0.1845
    
    If profitMargin > 0.15 Then
        Cells(6, 1).Value = "Profit Margin (High): " & Format(profitMargin, "0.00%")
        Cells(6, 1).Font.Color = RGB(0, 128, 0) ' Green
    Else
        Cells(6, 1).Value = "Profit Margin (Low): " & Format(profitMargin, "0.00%")
        Cells(6, 1).Font.Color = RGB(255, 0, 0) ' Red
    End If
End Sub

8.3 Date and Time Formatting

The Format function is particularly powerful for displaying dates and times:

Table 8.3: Date/Time Format Examples
Example Description Output (for 15-Jul-2023 14:30:45)
Format(Now, "mm/dd/yyyy") Short date format 07/15/2023
Format(Now, "dd-mmm-yyyy") Date with abbreviated month 15-Jul-2023
Format(Now, "dddd, mmmm dd, yyyy") Long date format Saturday, July 15, 2023
Format(Now, "hh:mm:ss AM/PM") 12-hour time format 02:30:45 PM
Format(Now, "yyyy-mm-dd hh:mm:ss") ISO format (sortable) 2023-07-15 14:30:45

Practical Example 8.3

Sub FormatDateTimeExamples()
    Dim currentTime As Date
    currentTime = Now
    
    ' Format current date/time in different ways
    Range("A1") = "Short Date: " & Format(currentTime, "mm/dd/yyyy")
    Range("A2") = "Long Date: " & Format(currentTime, "dddd, mmmm dd, yyyy")
    Range("A3") = "Time: " & Format(currentTime, "hh:mm:ss AM/PM")
    Range("A4") = "Combined: " & Format(currentTime, "mmmm dd, yyyy hh:mm AM/PM")
    Range("A5") = "ISO Format: " & Format(currentTime, "yyyy-mm-dd hh:mm:ss")
    
    ' Calculate and format time differences
    Dim startTime As Date, endTime As Date
    startTime = #9:15:00 AM#
    endTime = #4:45:30 PM#
    
    Range("A7") = "Work Duration: " & _
                  Format(endTime - startTime, "hh"" hrs ""mm"" mins""")
End Sub

8.4 Advanced Formatting Techniques

Conditional Formatting with VBA

Sub ApplyConditionalNumberFormatting()
    Dim rng As Range
    Set rng = Range("B2:B10") ' Assuming these cells contain numbers
    
    For Each cell In rng
        Select Case cell.Value
            Case Is >= 1000000
                cell.NumberFormat = "$#,##0.0,,""M""" ' Display as millions
            Case Is >= 1000
                cell.NumberFormat = "$#,##0.0,""K""" ' Display as thousands
            Case Else
                cell.NumberFormat = "$#,##0.00" ' Regular currency format
        End Select
    Next cell
End Sub

Formatting Phone Numbers

Function FormatPhoneNumber(phone As String) As String
    ' Format 10-digit numbers as (XXX) XXX-XXXX
    If Len(phone) = 10 And IsNumeric(phone) Then
        FormatPhoneNumber = Format(phone, "(000) 000-0000")
    Else
        FormatPhoneNumber = phone ' Return original if not 10 digits
    End If
End Function

Summary: Key Points About VBA Formatting Functions

  1. Predefined formats like "Currency" and "Percent" provide quick, standardized formatting
  2. Custom formats offer precise control over number display using symbols like #, 0, and %
  3. The Format function doesn't change the actual value, only its display representation
  4. Date/time formatting is particularly powerful with many built-in and custom options
  5. Combine formatting with conditional logic to create dynamic displays
  6. Always consider locale differences when formatting numbers, dates, and currencies
  7. For worksheet cells, you can also use Range.NumberFormat property for persistent formatting

Best Practices for Formatting in VBA

  • Be consistent with number of decimal places throughout your application
  • Consider your audience's regional settings for date and number formats
  • Use custom formats sparingly - they can make maintenance more difficult
  • When displaying percentages, make it clear whether you're formatting a decimal (0.15 → 15%) or already a percentage (15 → 1500%)
  • For financial applications, always use proper rounding before formatting

🔗 Related Resources



Related Lessons

Copyright ® 2023 Dr. Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page