Excel VBA Set Cell Format to Date: A Complete Guide
In this article
To set a cell format to a date in Excel VBA, use the .NumberFormat property of the Range object. For example, the line Range("A1").NumberFormat = "mm/dd/yyyy" instantly converts the display of cell A1 to a standard date format. This approach is essential for developers who need to ensure that numeric date values—which Excel stores as serial numbers—are readable by human users without requiring manual formatting after a macro runs.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- The Developer tab enabled in the Excel Ribbon (found via File > Options > Customize Ribbon).
- Basic familiarity with opening the Visual Basic Editor (Alt + F11).
- A dataset containing either date-like strings or serial numbers that require formatting.
Step-by-Step
- Open the Visual Basic Editor: Launch your Excel workbook. Press
Alt + F11on your keyboard to open the VBA Editor window. - Insert a New Module: In the Project Explorer on the left, right-click on your workbook name, select
Insert, and then clickModule. This creates a clean space to write your procedure. - Declare Your Subroutine: Type
Sub FormatCellsAsDate()at the top of the module to begin your macro. - Define the Target Range: Decide which cells need formatting. You can target a single cell, a specific range, or a dynamic selection. For a specific range, use
Set myRange = Range("B2:B100"). - Apply the NumberFormat Property: Use the
.NumberFormatproperty to assign the date style. For a standard US date, writemyRange.NumberFormat = "m/d/yyyy". If you prefer a long date format, usemyRange.NumberFormat = "dddd, mmmm dd, yyyy". - Handle Data Conversion (Optional but Recommended): If the cells contain text that looks like a date but isn't recognized as a number, formatting alone won't work. You must first convert the value. Use
myRange.Value = CDate(myRange.Value)within a loop to ensure Excel treats the content as a true date serial number before applying the format. - Run the Macro: Close the VBA editor or press
F5while the cursor is inside the subroutine to execute the code. - Verify the Result: Return to your Excel sheet and check that the cells now display the date in your chosen format.
Common Mistakes
One of the most frequent errors is confusing the .NumberFormat property with the Format() function. The .NumberFormat property changes how the cell looks in the spreadsheet while keeping the underlying value as a number. The Format() function, however, converts a value into a string. If you use the Format() function to put a date into a cell, Excel often treats that result as text, which means you cannot use it in date-based calculations or pivot tables. Always use .NumberFormat for cell styling.
Another common mistake is neglecting the regional settings of the computer running the macro. While VBA generally handles the "mm/dd/yyyy" syntax well, some users attempt to use local language names for months or days within the string, which can cause the format to fail or revert to "General" if the workbook is opened on a machine with different locale settings. Sticking to standard format codes (m, d, y) is the safest path for global compatibility.
Finally, users often forget that formatting a cell does not change the data type of the content. If a cell contains the text "January 1st, 2023" written as a literal string, applying .NumberFormat = "mm/dd/yyyy" will do absolutely nothing. The cell must contain a valid Excel date serial number for the formatting property to trigger a visual change.
Tips
To make your code more robust, use ActiveSheet.UsedRange instead of hard-coded cell references like "A1:A10". This ensures that your macro adapts to the size of your data automatically. For example, Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns("B")).NumberFormat = "mm/dd/yyyy" will format every used cell in column B regardless of how many rows exist.
If you need to apply a date format that matches the user's system settings exactly, you can use the Application.International object. However, for most automation tasks, explicitly defining the format string is preferred because it ensures consistency across all reports generated by the tool.
For better performance on large datasets (thousands of rows), avoid looping through cells one by one to apply formatting. Instead, apply the .NumberFormat to the entire range at once. VBA is significantly faster when performing a single operation on a large block of cells than when repeating a small operation thousands of times.
Troubleshooting
If the cells still appear as numbers (e.g., 45123) after running your code, it means the .NumberFormat was not applied correctly or was overwritten. Check your code for any lines that set the format to "General" later in the script. Also, ensure you are not accidentally applying the format to a range that is currently merged, as merged cells can sometimes behave unpredictably with range-based formatting.
If the cells show a series of hashtags (#######), this usually does not mean your VBA code failed. Instead, it means the column is too narrow to display the date format you chose. To fix this, add myRange.Columns.AutoFit to your macro immediately after the formatting line.
If the date does not change and the value remains as text, try using the Text To Columns method via VBA to force Excel to recognize the text as dates. You can simulate this by using Range("A1:A100").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, FieldInfo:=Array(1, xlDMYFormat). This converts the underlying data type to a date, allowing the .NumberFormat property to finally take effect.
Frequently Asked Questions
How do I format a cell to show only the month and year?
You can use the format string "mmm-yyyy" for a shortened month (e.g., Jan-2023) or "mmmm yyyy" for the full month name (e.g., January 2023).
Can I apply date formatting to a specific column in a Table (ListObject)?
Yes. Instead of using Range, you can reference the table column specifically using ActiveSheet.ListObjects("Table1").ListColumns("DateColumn").DataBodyRange.NumberFormat = "mm/dd/yyyy".
What is the difference between "m" and "mm" in the format string?
A single "m" displays the month without a leading zero (1, 2, ... 12), while "mm" ensures a two-digit month (01, 02, ... 12).
How do I clear the date format and return it to normal?
Set the .NumberFormat property to "General". For example: Range("A1").NumberFormat = "General".
Does this work for dates that include time?
Yes, but you must include time codes in the string. Use "mm/dd/yyyy hh:mm:ss" to display both the date and the precise time.
Will the format persist after I save and close the workbook?
Yes, .NumberFormat changes the cell's properties in the workbook itself. Once the macro runs and the file is saved, the formatting remains until it is manually changed or another macro modifies it.