Excel VBA Format Function
Format function in VBA is used to format the given values in the desired format, this function can be used for formatting dates or numbers or any trigonometrical values, this function has basically two mandatory arguments, one is the input which is taken in the form of a string and the second argument is the type of format we want to use for example if we use Format (.99,” Percent”) this will give us the result as 99%.
In VBA, we need to use the function called “FORMAT” to apply to format to cells. Excel formatting is one of the important concepts to master. The common formatting techniques we all use in our daily work are “date format, time format, number formatting, and other important formatting codes.” In regular excel worksheets, we just simply hit the format excel cell option and perform the formatting duty by applying the appropriate formatting code. However, in VBA, this isn’t that straight forward as our worksheet technique.
- Expression: This is nothing but the value we want to format. In VAB technicality, it is called as Expression.
- [Format]: What is the format you want to apply to the expression you have selected? We have two kinds of formatting here, one is the user-defined format, and the second one is the built-in format.
Here we have VBA Date Formats, Number Formats, and Text Formats.
VBA Date Formats have a short date, long date, medium date, and general date.
Number Formats have Currency, Standard, Percentage, Scientific, Yes or No, True or False, and On or Off.
- [First Day of the Week]: What is the first day of your week? We can select any day from the list. Below is the list of days and appropriate codes.
- [First Week of the Year]: What is the first week of the year? This specifies the week that should be used as the very first week of the year.
How to Use?
Ok, let’s apply this function practically to understand the functionality of the FORMAT function. Assume you have the number 8072.56489, and you want to apply number formatting to it. Follow the below steps to apply number formatting to it.
Step 1: Start an excel macro and define the variable as a “string” data type.
Sub Worksheet_Function_Example1() Dim K As String End Sub
Step 2: Assign a value to k as our number i.e., 8072.56489
Sub Worksheet_Function_Example1() Dim K As String K = 8072.56489 End Sub
Step 3: Show “k” value in the VBA message box.
Sub Worksheet_Function_Example1() Dim K As String K = 8072.56489 MsgBox K End Sub
Step 4: If you run this macro, we will get the below result.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
The result is as it is, we assigned the value to variable “k.” But we need to apply some formatting to this number to make it beautiful.
Step 5: Instead of directly assigning a value to “k,” let’s use the FORMAT function.
Sub Worksheet_Function_Example1() Dim K As String K = Format( MsgBox K End Sub
Step 6: Now for Expression, assign the number 8072.56489.
Sub Worksheet_Function_Example1() Dim K As String K = Format(8072.56489, MsgBox K End Sub
Step 7: In the formatting option, we can either use a built-in format, or we can use our own formatting code. Now I will use a built-in formatting style as “Standard.”
Sub Worksheet_Function_Example1() Dim K As String K = Format(8072.56489, "Standard") MsgBox K End Sub
Step 8: Now run this code and see the result of the message box.
Ok, we have got comma (,) as thousand separators, and decimal is rounded up to two digits only.
Like this, we can use many other built-in formatting styles to apply the formatting. Below are some of the codes I have applied.
#1 – Currency Format
Sub Worksheet_Function_Example2() Dim K As String K = Format(8072.56489, "Currency") MsgBox K End Sub
#2 – Fixed Format
Sub Worksheet_Function_Example3() Dim K As String K = Format(8072.56489, "Fixed") MsgBox K End Sub
#3 – Percent Format
Sub Worksheet_Function_Example4() Dim K As String K = Format(8072.56489, "Percent") MsgBox K End Sub
#4 – User-Defined Formats
Ok, now we will see some of the user-defined formats.
Sub Worksheet_Function_Example5() Dim K As String K = Format(8072.56489, "#.##") MsgBox K End Sub
Sub Worksheet_Function_Example5() Dim K As String K = Format(8072.56489, "#,##.##") MsgBox K End Sub
#5 – Date FORMAT
We have seen some of the important numbers of formatting techniques. Now we will have to use the FORMAT function to format the date in VBA.
I have written code to show the result of the date through the variable.
Sub Worksheet_Function_Example6() Dim K As String K = 13 - 3 - 2019 MsgBox K End Sub
When I run this code, I would not get an accurate date. Rather result is pathetic.
In order to get the accurate dates, we need to assign the date format to it. The first thing is we need to do supply the date in double-quotes and apply the date format.
Sub Worksheet_Function_Example6() Dim K As String K = Format("10 - 3 - 2019", "Long Date") MsgBox K End Sub
I run this code now, and I will get a proper long date.
“Long Date” is a built-in format. Similarly, you can use “short date” and “medium data” options.
Things to Remember
- The value returned by the FORMAT function is the string.
- We can also use our own date, time, and number formatting codes, like how we use in worksheet formatting.
- FORMAT is a VBA function and available only in VBA, not in the worksheet.
This has been a guide to VBA Format Function. Here we learned how to use VBA Format Function for currency, fixed, percentage, and date formatting along with some practical examples and a downloadable excel template. Below are some useful excel articles related to VBA –