Consulting

Results 1 to 9 of 9

Thread: What Quarter is the Date?

  1. #1

    Red face What Quarter is the Date?

    My program needs to figure out which quarter today currently is, along with hundreds of other dates. The contingency is simple; (January, February, and March) = Q1.... (April, May , June = Q2).... etc.

    I was hoping for something as simple as; If Month = "February" then Quarter = Q1. I tried using Today() in excel with a customized format of "mmmm". The output in Excel is "February" but VBA still recognizes it as a full date or long number. It does not recognize just "February". So my simple logic will not work.

    Any suggestions?

    Thanks!
    Kaela

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Does it need to be VBA?

    The following set of native formulas should work (where your date is in A1)
    PHP Code:
    =ROUNDDOWN(MONTH(A1)/(4*10/12),0)+
    HTH,
    Last edited by Aussiebear; 04-23-2023 at 04:55 PM. Reason: Added code tags
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by Kindly_Kaela
    I was hoping for something as simple as; If Month = "February" then Quarter = Q1. I tried using Today() in excel with a customized format of "mmmm". The output in Excel is "February" but VBA still recognizes it as a full date or long number. It does not recognize just "February". So my simple logic will not work.
    Hey Kaela,

    I'm assuming you're reading cells that you custom formated the cell to display the month of Today(). You could do 2 things in this case:

    1. If you read Range().Text, you will get the string i.e. "February" and VBA would read it as such. If you use Range() then it would return the date.
    2. If you used the formula Text(Today(),"mmmm") VBA would read Range() as "February" as a string because the formula would return a string.

    If you want a VBA answer you could use this:
    Public Sub Test1()
    Select Case Format(Date, "m")
        Case 1 To 3 ' Quarter 1
            Debug.Print "Quarter 1"
        Case 4 To 6 ' Quarter 2
            Debug.Print "Quarter 2"
        Case 7 To 9 ' Quarter 3
            Debug.Print "Quarter 3"
        Case Else
            Debug.Print "Quarter 4"
    End Select
    End Sub
    Hope this helps!

    And Ken, nice formula
    Last edited by Aussiebear; 04-23-2023 at 04:57 PM. Reason: Adjusted the code tags




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Formula
    PHP Code:
    =INT((MONTH(A1)+2)/3
    VBA
    (Month(the_date) + 2) \ 3
    Last edited by Aussiebear; 04-23-2023 at 04:57 PM. Reason: Adjusted the code tags

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just to be different
    PHP Code:
    =QUOTIENT(MONTH(A1),3.1)+
    Last edited by Aussiebear; 04-23-2023 at 04:58 PM. Reason: Added code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Nice guys. Bob, you're the king of efficiency.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Thank you Ken, think I will go and lie down now to save energy.

  8. #8
    Fantastic replies! Thanks everyone!

  9. #9
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by xld
    VBA

    (Month(the_date) + 2) \ 3
    Great use of the \ operator. I've never used it before cause I never ran into a case where I thought I'd need it. Nice example, Bob
    Last edited by Aussiebear; 04-23-2023 at 04:59 PM. Reason: Adjusted the code tags




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •