How to Calculate Leap Year With Microsoft Visual Basic

Microsoft Visual Basic has a variety of built-in functions, including for calculating and converting dates. A little thought applied to the definition of a leap year, combined with the Visual Basic IsDate function, will enable you to calculate leap years with Visual Basic.

Things You'll Need

  • Microsoft Word, Excel or other program containing the Visual Basic IDE (integrated development environment)
Show More

Instructions

    • 1

      Open up Microsoft Word and create a document called "LeapYear.doc." Enter the MS Word Visual Basic IDE by pressing Alt-F11.

      Select the correct module in which to enter program code. In the Project pane at the left of the IDE window, click the plus (+) sign to the left of the item "Project (LeapYear)." Click the plus sign of the folder "Microsoft Word Objects," which appears immediately under "Project (LeapYear)." Double-click "ThisDocument."

    • 2

      Enter the program code. In the blank code window, type or copy and paste the following program code:

      Public Function IsLeapYear(sYear As Variant)
      IsLeapYear = IsDate(sYear)
      End Function

      Public Sub NextLeapYear ()
      Dim sYear, sDate, msg
      Dim yr, startYear, nIter
      Dim myDate

      'Walk up from the given year until we find a leap year

      startYear = 1981

      yr = startYear
      nIter = 0
      myDate = "2/29/" & yr

      Do While ((IsLeapYear(myDate) = False) And nIter < 10)
      nIter = nIter + 1
      yr = yr + 1
      myDate = "2/29/" & yr

      Loop

      If (nIter < 10) Then
      msg = "The next leap year after " & startYear & " is " & yr
      MsgBox (msg)
      End If

      End Sub

    • 3

      Change the "startYear" variable to contain the year you want to calculate the next leap year for. Position the cursor anywhere inside the NextLeapYear function and press F5. The program executes, revealing the next leap on or before the year you entered.

    • 4

      Create the user interface. Select the complete program code you typed and cut it to the Clipboard (Ctrl-X). Click the "Project (LeapYear)" item in the Project pane again, then go to the "Insert" menu and choose "User Form." Drag and drop a textbox and a command button onto the form, with the button positioned under the textbox.

      Double-click the button and type (do not copy and paste) the following code:
      CommandButton1_Click function:
      MsgBox (NextLeapYear(TextBox1.Text))

      Position the cursor in any open code space outside the CommandButton1_Click function. Paste the code you previously cut to the Clipboard.

    • 5

      Change NextLeapYear to be a function. Change the opening line of NextLeapYear to read as follows:
      Public Function NextLeapYear(startyear)

      Delete the startYear variable from among the several Dim statements, and delete the line startyear=1981.

      Replace the msg variable with the name of the function (NextLeapYear) in the statement msg = "The next leap..."

      Delete the MsgBox statement that follows that "NextLeapYear =" statement. Verify that the last three lines of code in function NextLeapYear read as follows.

      If (nIter < 10) Then
      NextLeapYear = "The next leap year after " & startyear & " is " & yr
      End If

    • 6

      Use your leap year program. While inside the UserForm1 code window, press F5, enter a year in the text box, and press the button.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured