How to Calculate Business Days in Visual Basic

How to Calculate Business Days in Visual Basic thumbnail
Business days can be difficult to calculate.

Given 52 weeks in a year and five weekdays per week, generally considered the same as business days, a year should contain 52 times five, or 260, business days. This is not always true as seen when you compare calendars for multiple years. What was a business day in one year may not be in the next. Holidays impact the number of business days in a year as well. To get a more exact number of business days, use this small Visual Basic program.

Things You'll Need

  • Visual Basic 6.0
Show More

Instructions

    • 1

      Create a form in Visual Basic to hold the user interface for this program. Open Visual Basic and select "Standard EXE" from the template display. Double-click on the controls listed below from the "Toolbox" you see on the left of the screen. As each one appears on the form, click on it and drag to the specified location.

      Checkbox1: Drag to upper left of form
      Checkbox2: Drag to upper right of form
      Label1: Drag below Checkbox1
      Textbox1: Drag below Checkbox2
      CommandButton1: Center below above controls
      Label2 : Center below CommandButton1
      CommandButton2: Center below Label2

    • 2

      Add captions in the "Properties" box on the right of the screen for each of these controls. Do this by clicking on each, which brings up the selected control in the "Properties" box. The "Caption" property appears in alphabetical order in the properties list. Change the default caption or text as listed below. Use the drag handles on the form controls to accommodate the added text. Do not use the quotation marks below.

      Checkbox1 caption: "Subtract Federal Holidays?"
      Checkbox2 caption: "Leap Year?"
      Label1 caption: "How many additional holidays?"
      Textbox1: Delete the "Text" property in the list.
      CommandButton1: "Calculate Business Days". Change the "(Name)" property to "Calculate".
      Label2: Delete the caption if present.
      CommandButton2: "Exit".

    • 3

      Double-click on the "Calculate Business Days" button to open the code window. When you do this two lines of code display by default, "Private Sub Calculate_Click()" and "End Sub." Set up the first part of coding to calculate business days by entering the following lines between these two default lines:

      Dim EndDate, CurDate As Date
      Dim BusDay, TotBusDays, CalcYear As Integer
      TotBusDays = 0
      CalcYear = InputBox("What year?")
      CurDate = "1/1/" & CalcYear
      EndDate = "12/31/" & CalcYear
      Do Until CurDate = EndDate
      BusDay = Format(CurDate, "w")
      Select Case BusDay
      Case "2" To "6"
      TotBusDays = TotBusDays + 1
      Case "1"
      'Do nothing
      Case "2"
      'Do nothing
      End Select
      CurDate = DateAdd("d", 1, CurDate)
      Loop

    • 4

      Add additional code to compensate for holidays. Ten federal holidays usually mean 10 less working days even when the holiday falls on a weekend because most employers grant either a Friday or Monday in compensation. If this doesn't apply in your case, modify the value of the "How many additional holidays" entry box when the program runs. For example, if you do not get days off for some holidays, reduce or enter a negative number in that box. Type the following code directly below the last line in Step 3.

      If Check1.Value = vbChecked Then
      TotBusDays = TotBusDays - 10
      End If
      If Check2.Value = vbChecked Then
      TotBusDays = TotBusDays + 1
      End If
      If Text1.Text <> "" Then
      AddHol = Text1.Text
      TotBusDays = TotBusDays - AddHol
      End If
      Label1.Caption = "There are " & TotBusDays & " business days in " & CalcYear

    • 5

      Give the user an easy way to exit the program. On the form, double-click on the "Exit" button. Enter the word "End" between the "Private Sub Command2_Click()" and "End Sub" you see in the code section.

    • 6

      Click on the form itself and change the Caption property of the form to read "Calculate Business Days". Click "File," and then "Save Project As." Chose a folder for the program and name the project "Calculate Business Days". Click "OK," and then press "F5" to run the program.

Tips & Warnings

  • To make this into an executable program usable without loading Visual Basic, click on "File," and then "Make Calculate Business Days.exe." Choose where to save this and then click "OK."

  • Accounting for all the variables that affect business days, which do not occur on an easily predictable basis or even every year, requires more complicated coding beyond the scope of this article. For this reason, this simple program is not recommended for important financial or legal situations

Related Searches:

References

Resources

  • Photo Credit business environment image by Szymon Apanowicz from Fotolia.com

Comments

You May Also Like

Related Ads

Featured