Format String in Excel VBA: The Complete Guide

VBA & macros · Updated November 23, 2026

The Format function in Excel VBA converts a value into a formatted string using a pattern you supply, for example Format(1234.5, "#,##0.00") returns "1,234.50". It works with numbers, dates, times, and strings, and it is the primary tool for controlling how data appears in message boxes, cell values, and user forms.

What the Format Function Does

VBA’s Format function is similar to Excel’s TEXT worksheet function but is used inside macros. Its syntax is:

Format(expression, [format], [firstdayofweek], [firstweekofyear])

Only the first argument is required. The format argument is a string that tells VBA how to display the value. If you omit it, VBA uses the system’s default format for that data type. The function always returns a string, even if the original value was a number or date.

For example, with a cell containing the date 5/14/2024, the code:

Dim d As Date
d = Range("A1").Value
MsgBox Format(d, "mmmm d, yyyy")

displays May 14, 2024. The original cell value remains a date; the formatting only affects the string output.

Number Format Patterns

Number formats use placeholders that control digits, grouping, and decimals. The most common are:

  • 0 – always shows a digit, even if it’s zero (e.g., Format(5, "00") → "05")
  • # – shows a digit only if there is one (e.g., Format(5, "##") → "5")
  • . – decimal point
  • , – thousands separator
  • % – multiplies by 100 and adds a percent sign
  • E+ or e- – scientific notation

A common pattern is "#,##0.00", which shows two decimals and a comma separator. For negative numbers, you can add a second section after a semicolon: "#,##0.00;(#,##0.00)" displays negatives in parentheses. A third section handles zeros, and a fourth handles text.

Example: Format(-1234.5, "#,##0.00;(#,##0.00)") returns "(1,234.50)".

Date and Time Format Patterns

Dates and times use letter codes that VBA recognizes:

  • d – day (1–31), dd – two-digit day, ddd – short weekday name, dddd – full weekday name
  • m – month number, mmm – short month name, mmmm – full month name
  • yy – two-digit year, yyyy – four-digit year
  • h – hour, n – minute, s – second
  • AM/PM – 12-hour clock; without it, VBA uses 24-hour time

For example, Format(Now, "dddd, mmmm d, yyyy h:mm AM/PM") returns something like "Tuesday, May 14, 2024 3:45 PM". Note that m is month when used with dates, but minute when used after an hour code. So h:mm means hour and minute, while mm-dd means month and day.

You can also use named formats like "Long Date", "Short Date", "Long Time", or "General Number". These depend on the Windows regional settings, so they may produce different results on different computers.

String Formatting and the Format$ Function

For strings, Format can force case or add characters. Use "@" to place a character, and & to leave a space. For example, Format("abc", "@@@-@@") returns "abc- " (with two trailing spaces). More commonly, you’ll use UCase or LCase for case conversion instead.

VBA also provides Format$, which returns a string type directly. Format returns a Variant, which can be slightly slower and may cause type-conversion issues in some contexts. For most uses, Format$ is preferred when you know the result is text. The syntax is identical: Format$(1234.5, "#,##0.00").

How to Use Format in Real Code

You’ll often use Format when writing values to cells, building messages, or creating file names. Here’s a practical example that formats a date and a number into a file name:

Sub SaveReport()
Dim reportDate As Date
Dim salesTotal As Double
Dim fileName As String

reportDate = Date
salesTotal = 12345.678

' Format date as YYYYMMDD and number with two decimals
fileName = "Sales_" & Format(reportDate, "yyyymmdd") & "_" & Format(salesTotal, "0.00") & ".xlsx"

' Save a copy of the workbook before running this macro
ThisWorkbook.SaveCopyAs fileName
End Sub

This creates a file like Sales_20240514_12345.68.xlsx. Notice that the macro saves a copy, which is a good habit because macros cannot be undone with Ctrl+Z.

When writing to a cell, you can set the cell’s NumberFormat property instead of using Format on the value. For example:

Range("B2").NumberFormat = "#,##0.00"
Range("B2").Value = 1234.5

This keeps the cell as a number, so you can still perform calculations on it. Using Format to write a string to a cell converts it to text, which can break later math.

Common Problems and Misconceptions

One frequent mistake is confusing Format with FormatConditions (conditional formatting). Format is a function; FormatConditions is a collection of conditional formatting rules. They are unrelated.

Another issue is locale: Format uses the Windows regional settings for things like date separators and decimal points. If your code runs on a system with a different locale, Format(1234.5, "#,##0.00") might return "1.234,50" (comma as decimal). To force a specific separator, you can use Replace after formatting, or use the Format function with named formats that respect locale.

A third problem is using Format on an empty value. If you pass Empty or Null, Format returns "" (empty string) or "Null" respectively, which may not be what you expect. Always check for IsNull or IsEmpty first if your data might be blank.

Finally, remember that Format is for display, not for changing the underlying value. If you need to round a number, use Round or WorksheetFunction.Round instead.

Alternatives to Format

For simple number padding, you can use String and Len to manually pad, but Format is more concise. For dates, you can also use DatePart to extract components and build a string manually, but that’s more verbose.

If you’re working with Excel cells, setting NumberFormat is usually better than using Format because it preserves the data type. For text manipulation, VBA’s Replace, Left, Right, and Mid functions are often more appropriate than Format.

For very complex formatting, you can use the TEXT worksheet function via Application.WorksheetFunction.Text, but it has the same locale limitations. In modern Excel (Microsoft 365), you can also use TEXTSPLIT and TEXTJOIN for string manipulation, but those are worksheet functions, not VBA.

Frequently Asked Questions

How do I format a number with leading zeros in VBA? Use the 0 placeholder. For example, Format(7, "000") returns "007". If you need a fixed length, count the zeros.

Why does my date format show “m” as minutes? Because m is interpreted as month when it follows a day or year, but as minute when it follows an hour. To avoid confusion, use mm for minutes and mmm or mmmm for months.

Can I use Format to change a cell’s display without changing its value? No. Format returns a string, so if you put that string into a cell, the cell becomes text. To change display only, set the cell’s NumberFormat property instead.

What’s the difference between Format and Format$? Format$ returns a String data type, while Format returns a Variant. Format$ is slightly faster and avoids Variant overhead, but otherwise they work the same.

How do I format a negative number in parentheses? Use a two-section format: Format(-5, "#,##0.00;(#,##0.00)") returns "(5.00)". The first section is for positive, the second for negative.

Does Format work on Mac Excel? Yes, but the regional settings may differ, so date and number separators can vary. The function itself is identical in VBA for Mac.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.