How to Format data with custom number formats using VBA in Excel?
Tags: toformatdatawithcust, vbamacros
Published: 2021-10-08 09:17:44

---
title: How to Format data with custom number formats using VBA in Excel?
published: true
date: 2021-10-08 06:14:51 UTC
tags: ToFormatDataWithCust,VBAMacros
canonical_url: https://geekexcel.com/how-to-format-data-with-custom-number-formats-using-vba-in-excel/
---

In this article, you will learn how to  **format data with custom number formats using VBA**  in Microsoft Excel. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## Example

- Firstly, you need to **create a sample data** with **name, product ID, product price, quantity sold and total sales**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-18-1024x359.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Option Explicit
Sub Formatting()
'Declaring variables
Dim wks As Worksheet
Dim intRow As Long
Dim strCol As String
Dim txt As String
'Initializing the variables
Set wks = Worksheets("Format")
intRow = 4
'Looping in 16th column until empty value is found
Do Until IsEmpty(wks.Cells(intRow, 16))
 'Assigning the value of custom number format
 txt = wks.Cells(intRow, 17)
 'Looping and finding all the column number separated by comma(,)
 Do Until InStr(txt, ",") = 0
 'Getting the column number
 strCol = Left(txt, InStr(txt, ",") - 1)
 'Assigning the number format
 Columns(CInt(strCol)).NumberFormat = wks.Cells(intRow, 16).Value
 'Truncating the string for finding the next column number after the comma(,)
 txt = Right(txt, Len(txt) - InStr(txt, ","))
 Loop
 'Assigning the number format
 Columns(CInt(txt)).NumberFormat = wks.Cells(intRow, 16).Value
 intRow = intRow + 1
Loop
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Code-25-1024x895.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- Then, you need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/10/Run-the-Code-24.png)<figcaption>Run the Code</figcaption>

- Finally, macro will change the **formatting of the given data** according to the **specified custom number format** in Excel.

![](https://geekexcel.com/wp-content/uploads/2021/10/Output-17.png)<figcaption>Output</figcaption>

## Sum-Up

We hope that this article gives you guidelines on how to  **format data with custom number formats using VBA ** in Microsoft Excel. ** ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Related Articles:**

- **[How to Identify and Convert Date Format in Excel Office 365?](https://geekexcel.com/how-to-identify-and-convert-date-format-in-excel-office-365/)**
- **[Excel Formulas to Add Workdays to Date with Custom Weekends!!](https://geekexcel.com/excel-formulas-to-add-workdays-to-date-with-custom-weekends/)**
- **[Excel Formulas to Generate a Series of Dates by Custom Days ~ Easily!!](https://geekexcel.com/excel-formulas-to-generate-a-series-of-dates-by-custom-days/)**
- **[How to Create Custom Lists and Fill Cells with them in Excel 365?](https://geekexcel.com/how-to-create-custom-lists-and-fill-cells-with-them-in-excel-365/)**
- **[How to Create & Use Excel Custom ADD Function in Office 365?](https://geekexcel.com/how-to-create-custom-add-function-in-excel-365/)**