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.
The predefined Format function provides standard formatting options that are commonly used in business applications. The syntax is:
Format(expression, "style_argument")
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 |
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
When predefined formats don't meet your needs, you can create custom formats using special symbols:
Format(expression, "custom_format")
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 |
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
The Format function is particularly powerful for displaying dates and times:
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 |
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
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
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
Copyright ® 2023 Dr. Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page