# Adding a Custom Function That Returns the Quarter of the year for the given date

In this article, we will create a custom function that returns the quarter of the year for the given date.

Raw data consists of product sales data, which consists of Sales date, Product name and Quantity.

In this article, we want to find out total quantity of products sold out on quarterly basis. So, we want to find out quarter of the year for each date.

Code explanation

DatePart("q", Enter_Date)

We have used DatePart function of VBA to extract quarter of the year from the date.

```
Option Explicit

Function QuarterNum(Enter_Date As Date)

'Using DatePart function to extract quarter from given date
QuarterNum = DatePart("q", Enter_Date)

End Function

```

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at info@exceltip.com

1. "Hi Mohamed,

If you want a UDF (or an code) to be available to a given user at all times regardless of the workbook they are using, then put that code in their PERSONAL.XLS file.

If you want the code to always be available within a given workbook, regardless of the user, then you must put the code in that workbook.

As far as I know, it is impossible to do both, unless you are Microsoft (!)

HTH,

Alan"

2. "is there any way to save custom function as user-defind
function permenantly. (unique) i.e to use it at any time
with any woorksheet just like other functions."

3. "Hi Ernest,

Have you read the help screens in Excel?

If so, what specific questions do you have - what is not clear?

Thanks,

Alan."

4. For purposes where the quarter should be 1 for April-May-June, and so on( for example, for a financial year ), then simply alter this custom function so that it reads: QuarterNum = ((DatePart("q", Enter_Date) + 2) Mod 4) + 1 and it will return financial year quarter.

5. "Hi Mohamed,

If you want a UDF (or an code) to be available to a given user at all times regardless of the workbook they are using, then put that code in their PERSONAL.XLS file.

If you want the code to always be available within a given workbook, regardless of the user, then you must put the code in that workbook.

As far as I know, it is impossible to do both, unless you are Microsoft (!)

HTH,

Alan."

6. "is there any way to save custom function as user-defind
function permenantly. (unique) i.e to use it at any time
with any woorksheet just like other functions."

7. "Hi Ernest,

Have you read the help screens in Excel?

If so, what specific questions do you have - what is not clear?

Thanks,

Alan."

8. can you kindly tell me what Hlookup is and ho to use it in Excel?

9. For purposes where the quarter should be 1 for April-May-June, and so on( for example, for a financial year ), then simply alter this custom function so that it reads: QuarterNum = ((DatePart("q", Enter_Date) + 2) Mod 4) + 1 and it will return financial year quarter.

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.