+ Reply to Thread
Results 1 to 22 of 22

vba to find the month and week number from given date

  1. #1
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    vba to find the month and week number from given date

    Hi

    I need the help on attch file , in the attch file i have the date coulumn from this date column i need to calulate the month & week no. (like WEEK1,WEEK2..)

    The Week ( Monday to sunday) which need to be calculated is the week no. in the given month

    like for month of April the week1 is print in the week column for 6april to 12 april date and Week2 print for 13 April to 19 april

    same is calulated for all the moth
    Attached Files Attached Files
    Last edited by Anil2007; 04-24-2009 at 11:22 AM.

  2. #2
    Registered User
    Join Date
    04-22-2009
    Location
    Middle of Nowhere, USA
    MS-Off Ver
    Office 2007 (all of it!)
    Posts
    2

    Re: vba to find the month and week number from given date

    I'm not sure about the solution for the week problem, but at least for the month column, you don't need VBA at all, just a quick function.

    For example, you have 4/3/2009 in the A2 cell. Here's the code for your month column.

    Please Login or Register  to view this content.
    That will return the "serial number" of the month, otherwise known as the number 4 to us common folk.

    As for your week problem, I'm unsure. There is a function in Excel called WEEKNUM that will tell you the week of the year, but since the number of weeks in a month change, I'm not sure how to make a formula out of it. If I can think of a solution, I'll post here again.

    Hope this helps!

  3. #3
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by bandgeekndb View Post
    I'm not sure about the solution for the week problem, but at least for the month column, you don't need VBA at all, just a quick function.

    For example, you have 4/3/2009 in the A2 cell. Here's the code for your month column.

    Please Login or Register  to view this content.
    That will return the "serial number" of the month, otherwise known as the number 4 to us common folk.

    As for your week problem, I'm unsure. There is a function in Excel called WEEKNUM that will tell you the week of the year, but since the number of weeks in a month change, I'm not sure how to make a formula out of it. If I can think of a solution, I'll post here again.

    Hope this helps!
    Hi

    Thank for help . i am able to get the month but problem is that i am not able to get the week no. within the month

  4. #4
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    hi

    please help

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    How about this formula in the WEEK column starting at C2, then copy down:

    =CEILING((DAY(A2)+WEEKDAY(DATE(YEAR(A2),MONTH(A2),1),2))/7,1)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by JBeaucaire View Post
    How about this formula in the WEEK column starting at C2, then copy down:

    =CEILING((DAY(A2)+WEEKDAY(DATE(YEAR(A2),MONTH(A2),1),2))/7,1)
    HI

    Thnk for giving me the formula to calculate the week no. but my problem is that

    week is defined from Monday to sunday for example like in month of april09
    the date 6-april-09 Week number is 2 as week start from monday. The above formula return the week no. for 6-april-09 is 1

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    A little tweak then:

    =LOOKUP(7+DAY(A2)-WEEKDAY($A2,2),{0,8,15,22,29},{1,2,3,4,5})

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    And since this is the programming forum and you originally wanted a VBA solution, here's the same approach imbedded into a macro:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by JBeaucaire View Post
    And since this is the programming forum and you originally wanted a VBA solution, here's the same approach imbedded into a macro:
    Please Login or Register  to view this content.

    HI

    THE Above code is work but still this give the week number of any month starting from first date of the month my problem is that i need a week number 1 only if the month start date is of Monday

    Like Take a eample of Month April-09

    1April to 5April - Week 5 ( 'coz the week start from Wednesday)
    6 Apr to 12 April - Week1 ('coz Week start from Monday)

    In short the Week1 for week satrt frm Monday to Sunady
    Week2 for Next Monady to sunday and so on for each month

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    Ok, that's weird, and you said that and I missed it...So if the first of the month starts on a Tuesday, those first 6 days AREN'T week1, they're week?

    April 1 - April 5 - Week?
    April 6 - April 12 - Week1
    April 13 - April 19 - Week2
    April 20 - April 26 - Week3
    April 27- April30 - Week4

  11. #11
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by JBeaucaire View Post
    So if the first of the month starts on a Tuesday, those first 6 days AREN'T week1, they're week?

    April 1 - April 5 - Week?
    April 6 - April 12 - Week1
    April 13 - April 19 - Week2
    April 20 - April 26 - Week3
    April 27- April30 - Week4
    Yes you r wright they are week but that week is come under the prior month week in above case April1 - April5 is come under the Week5 of month March

  12. #12
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by Anil2007 View Post
    Yes you r wright they are week but that week is come under the prior month week in above case April1 - April5 is come under the Week5 of month March

    Hi

    i am attched the "weekday.xls"updated file where the week is calulated correctly as per the my requirement with the help of user defined function but problem is that if i use this method and apply this the function on cell and drag the formula to 20,000row where i calculated the week no. the excel will hang up

    I think this week.xls file can help you to understand my problem can it be possible that i get the solution in VBA code

    Thank
    Attached Files Attached Files
    Last edited by Anil2007; 04-23-2009 at 11:08 AM. Reason: attach the updated file

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    Ok, maybe this?
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by JBeaucaire View Post
    Ok, maybe this?
    Please Login or Register  to view this content.
    Hi
    Thanks now with the help of above code i can get the Week no. correctly
    One more question is that also possible the with the help of code i also get the correct month

    example Like

    1April 09 - Week5( This i easily get after running the above code)

    but for month column is that possible as this week 5 is not come under the Month April. so in month it will automatically goes under the month of march i.e in month column the code show the March-09 and the same will happened for all the date

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: vba to find the month and week number from given date

    You can get the week number by using WEEKNUM from the Analysis ToolPak

    In B1 = Month(A1)

    In C1 =WeekNum(A1,2) <- the two sets the first day of week as Monday
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  16. #16
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    Once more:
    Please Login or Register  to view this content.
    These are the worksheet formulas being used and then wiped:

    Month:
    =IF(AND(C2=5,DAY(A2)<29),MONTH(A2)-1,MONTH(A2))

    Week#:
    =IF(WEEKDAY(DATE(YEAR($A2),MONTH($A2),1),2)=1,LOOKUP(7+DAY($A2)-WEEKDAY($A2,2),{0,8,15,22,29},{1,2,3,4,5}),LOOKUP(7+DAY($A2)-WEEKDAY($A2,2),{0,8,15,22,29,36},{5,1,2,3,4,5}))

    Last edited by JBeaucaire; 04-23-2009 at 03:08 PM.

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    Quote Originally Posted by royUK View Post
    You can get the week number by using WEEKNUM from the Analysis ToolPak

    In B1 = Month(A1)

    In C1 =WeekNum(A1,2) <- the two sets the first day of week as Monday
    No, they're using monthly week numbers 1-5, not yearly.

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: vba to find the month and week number from given date

    To get the ISO Week Number - weeks run Monday to Sunday, first week is the week containins the first Thursday of the year

    =INT((A1-SUM(MOD(DATE(YEAR(A1-MOD(A1-2,7)+3),1,2),
    {1E+99,7})*{1,-1})+5)/7)

    Why does the OP need these Columns?

  19. #19
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: vba to find the month and week number from given date

    Quote Originally Posted by JBeaucaire View Post
    Once more:
    Please Login or Register  to view this content.
    These are the worksheet formulas being used and then wiped:

    Month:
    =IF(AND(C2=5,DAY(A2)<29),MONTH(A2)-1,MONTH(A2))

    Week#:
    =IF(WEEKDAY(DATE(YEAR($A2),MONTH($A2),1),2)=1,LOOKUP(7+DAY($A2)-WEEKDAY($A2,2),{0,8,15,22,29},{1,2,3,4,5}),LOOKUP(7+DAY($A2)-WEEKDAY($A2,2),{0,8,15,22,29,36},{5,1,2,3,4,5}))

    Thank you very much for provide me the right solution . Now with the help of above code i easily get the Week & Month

    Once again thank you for your hep

  20. #20
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    Sorry it took so long to get there....my bad.

    If that takes care of your need, be sure to EDIT your original post (Go Advanced) and mark the PREFIX box [SOLVED]

  21. #21
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: vba to find the month and week number from given date

    Quote Originally Posted by JBeaucaire View Post
    Month:
    =IF(AND(C2=5,DAY(A2)<29),MONTH(A2)-1,MONTH(A2))

    Week#:
    =IF(WEEKDAY(DATE(YEAR($A2),MONTH($A2),1),2)=1,LOOKUP(7+DAY($A2)-WEEKDAY($A2,2),{0,8,15,22,29},{1,2,3,4,5}),LOOKUP(7+DAY($A2)-WEEKDAY($A2,2),{0,8,15,22,29,36},{5,1,2,3,4,5}))
    Hello Anil/JB

    I see potential problems for both of these fomulas......

    The first one will return zero for some January dates, presumabaly that should be 12, I suggest

    =MONTH(A2-WEEKDAY(A2,3))

    where your date is in A2

    For the week number within the month I note that the above [JB] formula returns 4 for Thursday 30th April 2009 but 5 for the next day, Friday 1st May 2009. I know this is a new month but shouldn't the week number stay the same throughout the week? I suggest this formula to get the week number in the month:

    =INT((6+DAY(A2-WEEKDAY(A2,3)))/7)

    With this formula week 1 always starts on the first Monday of the month, all weeks run for 7 days.

    regards
    daddylonglegs

  22. #22
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba to find the month and week number from given date

    Hehe, I should've realized when I'd run that far down the field with those convulsing formulas that there was a simpler way....thanks DDL!

    Here's the better formulas inserted into the macro, Anil2007...definitely use this version:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1