+ Reply to Thread
Results 1 to 22 of 22

Calculating number of days

  1. #1
    Registered User
    Join Date
    12-09-2009
    Location
    Belgrade
    MS-Off Ver
    Excel 2010
    Posts
    17

    Question Calculating number of days

    Hello,I'm trying to make a VBA script that will calculate dynamically the number of days in a specific year for chosen month which will later be used for simple math.

    My table looks like this:

    A2 = 2019 (here I manually type in your number)
    A5 = is a data validation list with months (this is generated with VBA)
    A8 = is returned number of days in the selected month for 2019 here is my VBA code:

    Please Login or Register  to view this content.
    My knowledge of VBA is very basic and all though this is working is it possible to make it work, update results in A8 when I choose different item from the list?And if something is wrong with the code please do tell me... all the best G.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Calculating number of days

    How about this for starters.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    Quote Originally Posted by GOR4N View Post
    ... A8 = is returned number of days in the selected month ...
    Maybe formula ?
    In 'A8':

    =IF(OR(A5={"January","March","May","July","August","October","December"}),31,IF(OR(A5={"April","June","September","November"}),30,IF(A5="February",IF(MOD(A2,4)=0,29,28),"")))

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Calculating number of days

    Quote Originally Posted by bakerman2 View Post
    Please Login or Register  to view this content.
    Following your idea of using the Change event... I am not sure if some of the OP's spelling of the month names are typos, so I can't be sure the following will work of him or not. Anyway, the following code works here in the US... I think it should work in other locales as well (providing correctly spelled month names for that locale are used in cell A5) but I have no way of testing it.
    Please Login or Register  to view this content.


    Quote Originally Posted by porucha vevrku View Post
    Maybe formula ?
    In 'A8':

    =IF(OR(A5={"January","March","May","July","August","October","December"}),31,IF(OR(A5={"April","June","September","November"}),30,IF(A5="February",IF(MOD(A2,4)=0,29,28),"")))
    Of course, if the above code does in fact work correctly for the OP's locale, then this formula should also...

    =DAY(EOMONTH("1 "&A5&" "&A2,0))
    Last edited by Rick Rothstein; 12-03-2019 at 05:12 AM.

  5. #5
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    Quote Originally Posted by Rick Rothstein View Post
    ... if ... work correctly for the OP's locale ...
    Of course, after all, "January" is not "January" (etc.) in every language, it could be e.g. "Sausis", "Janvier", "Stud'yen'", "Siječanj", "Enero", "Gener", etc. ...

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Calculating number of days

    Quote Originally Posted by porucha vevrku View Post
    Of course, after all, "January" is not "January" (etc.) in every language, it could be e.g. "Sausis", "Janvier", "Stud'yen'", "Siječanj", "Enero", "Gener", etc. ...
    Which is why I said "providing correctly spelled month names for that locale are used in cell A5" in my first paragraph. I believe, but am not 100% sure, that Excel will convert text in the form... DayNumber, Space, MonthName, Space, Year... to a real date where the MonthName is correctly spelled for that locale.

  7. #7
    Registered User
    Join Date
    12-09-2009
    Location
    Belgrade
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Calculating number of days

    Hi all ... tnx for quick answers

    mr.bakerman2 ... for some reason this one didn't worked for me

    Quote Originally Posted by bakerman2 View Post

    Please Login or Register  to view this content.

    The second one from vevrku gave me some function error

    Quote Originally Posted by porucha vevrku View Post
    Maybe formula ?
    In 'A8':

    =IF(OR(A5={"January","March","May","July","August","October","December"}),31,IF(OR(A5={"April","June","September","November"}),30,IF(A5="February",IF(MOD(A2,4)=0,29,28),"")))
    What I had to do is replace , with ; and do some small changes to function
    In the end working function looks like this
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    For me it's working now as I wanted but if some of you guys are interested why errors are happening I am more than willing to do some debuging with you guys.

    Tnx again ... G.

  8. #8
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    or , or ; or \ or others - it depends on the system settings: date, separators, etc.

    Variants:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  9. #9
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,762

    Re: Calculating number of days

    The end part of your final formula is trying to calculate whether the year is a leap year or not, but you should be aware that a century year (1900, 2000, 2100 etc.) is NOT a leap year, even though it is divisible by 4, so you need to make a slight adjustment if you want to cope with those years correctly.

    Hope this helps.

    Pete

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Calculating number of days

    Quote Originally Posted by GOR4N View Post
    For me it's working now as I wanted but if some of you guys are interested why errors are happening I am more than willing to do some debuging with you guys.
    Did you ever try either the code or the formula that I posted in Message #4? I am curious if either of them work (as posted) in your locale.

  11. #11
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    Quote Originally Posted by Pete_UK View Post
    ... you should be aware that a century year (1900, 2000, 2100 etc.) is NOT a leap year ...
    Wow, Do you plan to use excel in year 2100, etc. ?

    ... then there will be no Excel and nothing from the Office also ... not to mention us ...

    Everything in Excel, including functions/formulas, is subject to restrictions

  12. #12
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,762

    Re: Calculating number of days

    We don't know what the OP is using this for - s/he talks about using the value "which will later be used for simple math".

    They might want to do this for the year 2000 - who knows ??

    Pete

  13. #13
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    ... something from before 1900 ... ... that would be fun ... e.g. February 25, 9999 BC ...

  14. #14
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Calculating number of days

    Just noting that there is a very simple test whether a year is a leap year or not...

    In VBA, test this... Month(DateSerial(Yr, 2, 29)) = 2

    In a formula, test this... MONTH(DATE(A2,2,29))=2

    Where Yr is a variable containing a year value and A2 is assumed to be a cell containing a year value.

  15. #15
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,762

    Re: Calculating number of days

    It's an easy modification to the earlier formula - instead of:

    … IF(MOD(A2;4)=0;29;28) ...

    near the end, you would have:

    … IF(MOD(A2;100)=0;28;IF(MOD(A2;4)=0;29;28)) …

    Hope this helps.

    Pete

  16. #16
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Calculating number of days

    Quote Originally Posted by Pete_UK View Post
    It's an easy modification to the earlier formula - instead of:

    … IF(MOD(A2;4)=0;29;28) ...

    near the end, you would have:

    … IF(MOD(A2;100)=0;28;IF(MOD(A2;4)=0;29;28)) …
    Or using what I posted in Message #14...

    28+(MONTH(DATE(A2,2,29))=2)

  17. #17
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    Quote Originally Posted by Pete_UK View Post
    … IF(MOD(A2;100)=0;28;IF(MOD(A2;4)=0;29;28)) …
    Leap year = True <=> ( (year mod 4 = 0) and (year mod 100 <> 0) ) or (year mod 400 = 0)

    ("https://en.wikipedia.org/wiki/Leap_year")

    But the formula will be more complicated, why complicate our life ?

  18. #18
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,152

    Re: Calculating number of days

    Quote Originally Posted by Rick Rothstein View Post
    ... the following code works here in the US ...
    Quote Originally Posted by Rick Rothstein View Post
    ... but am not 100% sure, that Excel will convert text ...
    Quote Originally Posted by GOR4N View Post
    ... I am more than willing to do some debuging with you guys ...
    We can always rack our brains

    https://docs.microsoft.com/en-us/off....msolanguageid
    https://docs.microsoft.com/en-us/ope...d-c3b0ee3dbc4a

    "January" in English is "January" => TEXT(DATE(2019, 1, 1),"[$-" & DEC2HEX(1033) & "] mmmm")
    "January" in Danish is "Januar" => TEXT(DATE(2019, 1, 1),"[$-" & DEC2HEX(1030) & "] mmmm")
    "January" in Lithuanian is "Sausis" => TEXT(DATE(2019, 1, 1),"[$-" & DEC2HEX(1063) & "] mmmm")
    etc.
    Of course, appropriate system separators should be included (, ; ...)

  19. #19
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Calculating number of days

    Quote Originally Posted by porucha vevrku View Post
    But the formula will be more complicated, why complicate our life ?
    Did you see Messages #14 and #16 yet? Checking if a year is a Leap Year or not is not complicated at all in Excel.

  20. #20
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,762

    Re: Calculating number of days

    We've heard very little from the OP (not since Post #7), so I think I shall unsubscribe from this thread.

    Pete

  21. #21
    Registered User
    Join Date
    12-09-2009
    Location
    Belgrade
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Calculating number of days

    i did try but it didn't work ... i don't get error but instead it keept me asking to "choose macro" dialog

  22. #22
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Calculating number of days

    In ThisWorkbook-module
    Please Login or Register  to view this content.
    In Standard-module.
    Please Login or Register  to view this content.
    In Worksheet-module where calculation is.
    Please Login or Register  to view this content.
    And then I'm 200% sure it works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Need help on calculating number of days between 2 days W.R.T different criterias
    By Vikramchetan in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-27-2019, 08:16 AM
  2. DAYS function: correctly calculating the number of days
    By dadpad in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-25-2015, 09:18 PM
  3. Calculating number of days
    By excelquestionz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-06-2015, 02:08 PM
  4. Calculating number of days, help.
    By DollyMixture in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-23-2014, 11:01 AM
  5. [SOLVED] Calculating the number of days
    By bremen22 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-09-2013, 12:06 PM
  6. calculating the number of days
    By Deanomcbeano in forum Excel General
    Replies: 12
    Last Post: 10-02-2012, 08:22 AM
  7. [SOLVED] Calculating number of days
    By Carlos in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-01-2005, 05:06 PM

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