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.

ArrowRawData

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.

ArrowOutput

Code explanation

DatePart("q", Enter_Date)

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

 

Please follow below for the code


Option Explicit

Function QuarterNum(Enter_Date As Date)

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

End Function

 

If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

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

Comments

  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. 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.

  4. "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."

  5. "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."

  6. "Hi Ernest,

    Have you read the help screens in Excel?

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

    Thanks,

    Alan."

  7. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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.