Consulting

Results 1 to 6 of 6

Thread: find number of work days in month

  1. #1
    VBAX Regular
    Joined
    Dec 2011
    Posts
    16
    Location

    find number of work days in month

    So I am trying to find the number of work days in a month given that the month is a user inputted integer.

    so this is what assigns the month

    [VBA]month = InputBox("Enter the month as an number (EX. If you want to enter for January then enter 1).", "Info", "99")[/VBA]

    then i want to find the nubmer of work days from month/1/current year to the end of the month.

    so i have

    [VBA]startdate = month / 1 / 12

    enddate = WorksheetFunction.EoMonth(startdate, 0)
    workdaysinmonth = Application.WorksheetFunction.NetworkDays(startdate, enddate)[/VBA]

    but it thinks i am trying to divide the month by one and by 12 and never gives me a date.

    its like i need something like

    [VBA]startdate = &month& #1/12#[/VBA]

    but that is not recogized by vba.

    please help

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    You should probably change the variable name as Month is a reserve word.

    [VBA]startdate = MyMonth & "/1/12"[/VBA]

    David


  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    If you were to use Option Explicit at the start of your code, it would pick up errors such as typos.undeclared variables and use of reserved words.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think you want to use DateSerial(..), as well as Option Explicit as well as explicitly Dim-ing your variables

    [VBA]
    Option Explicit
    Sub demo()
    Dim StartDate As Date, EndDate As Date
    Dim Mon As Long
    Mon = InputBox("Enter the month as an number (EX. If you want to enter for January then enter 1).", "Info", "99")

    StartDate = DateSerial(2012, Mon, 1)
    EndDate = DateSerial(Year(StartDate), Month(StartDate) + 1, 0)
    End Sub
    [/VBA]


    Paul

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You could also use

    [VBA] EndDate = StartDate + 31 - Day(StartDate + 31)
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    The last one from you "xld" seems quit simple and nice. Well. these was new thing that I get to learn here. These formula should mostly be used by people going to office. About me I used to calculate the working day at the start of every month. So it great to be the part of these thread and discussion.

Posting Permissions

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