Implementing VBA macro is the most effective, quickest and safest method to run any operation in Excel. In this article, we will show you how to format the number in Excel using VBA.
Download Workbook
You can download the free practice Excel workbook from here.
3 Methods to Format Number in Excel with VBA
Look at the following example. We stored the same numbers in both Column B and C so that when we format the number in Column C, you will know from the B Column in which format the number was before.
1. VBA to Format Number from One Type to Another in Excel
First, let’s know how to format the number 12345 from Cell C5 in our given dataset with VBA to Currency format.
Steps:
- Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.
- In the pop-up code window, from the menu bar, click Insert -> Module.
- Copy the following code and paste it into the code window.
Sub NumberFormat()
    Range("C5").NumberFormat = "#,##0.0"
    'This will format the number 12345 into a currency
End Sub
Your code is now ready to run.
- Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Play icon in the sub-menu bar to run the macro.
This code will format the number 12345 into a currency with a decimal value.
If you want to show the currency symbol in the cell then simply put the symbol before the code.
Sub NumberFormat()
    Range("C6").NumberFormat = "$#,##0.0"
    'This will format the number 12345 into currency with $ symbol
End Sub
For our case, we used the dollar ($) symbol. You can use any currency symbol that you want.
This code will format the number into currency with a dollar ($) symbol.
You can also convert this format of number into many other formats. Just follow the code below to transform the number into the format you require.
Sub NumberFormat()
'Original Number 12345
    Range("C5").NumberFormat = "#,##0.0"
    'This will format the number into a currency
    Range("C6").NumberFormat = "$#,##0.0"
    'This will format the number into a currency with $ symbol
    Range("C7").NumberFormat = "0.00%"
    'This will format the number into a percentage value
    Range("C8").NumberFormat = "#,##.00;[red]-#,##.00"
    'This will format the number into red color (Conditional Formatting)
    Range("C9").NumberFormat = "#?/?"
    'This will format the number into fractions
    Range("C10").NumberFormat = "0#"" Kg"""
    'This will format the number with text
    Range("C11").NumberFormat = "#-#-#-#-#"
    'This will format the number with separators
    Range("C12").NumberFormat = "#,##0.00"
    'This will format the number with commas and decimals if applicable
    Range("C13").NumberFormat = "#,##0"
    'This will format the number into thousands with commas if applicable
    Range("C14").NumberFormat = "#,##0.00"
    'This will format the number into millions
    Range("C15").NumberFormat = "dd-mmm-yyyy hh:mm AM/PM"
    'This will format the number into date & time
End Sub
VBA Macro
Overview
Read More: Excel Custom Number Format Multiple Conditions
2. Macro to Format A Range of Numbers in Excel
We have seen how to change the number format for a single cell. But if you want to change the format for a range of numbers then the VBA codes are pretty much the same as shown in the above section. This time instead of passing one single cell reference number inside the parentheses of the Range object, you have to pass the whole range (like this C5:C8) inside the brackets.
Sub NumberFormatRng()
    Range("C5:C8").NumberFormat = "$#,##0.0"
End Sub
This code will format a specific range of numbers from your dataset in Excel.
Read More: How to Format Number to Millions in Excel (6 Ways)
Similar Readings
- How to Put Parentheses for Negative Numbers in Excel
- Excel Round to 2 Decimal Places (with Calculator)
- How to Format a Number in Thousands K and Millions M in Excel (4 Ways)
- Custom Number Format: Millions with One Decimal in Excel (6 Ways)
- How to Change Number Format from Comma to Dot in Excel (5 Ways)
3. Embed VBA to Convert Number with Format Function in Excel
You can also use the Format function in Excel VBA to convert the numbers. The macro to do that is,
Steps:
- Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
- In the code window, copy the following code and paste it.
Sub NumberFormatFunc()
    MsgBox Format(12345, "#,##0.00")
End Sub
Your code is now ready to run.
You will get the formatted number in the message box.
Related Content: How to Convert Number to Percentage in Excel (3 Quick Ways)
Conclusion
This article showed you how to format the number in Excel with VBA. I hope this article has been very beneficial to you. Feel free to ask if you have any questions regarding the topic.