Jacob J. Walker's Blog

Scholarly Thoughts, Research, and Journalism for Informal Peer Review

Archive for February 6th, 2015

Some Short VBA Examples to Help Teach the Fundamentals of Programming

without comments

I am starting to teach Visual Basic for Applications in Excel tonight,
and wanted to have some code for the students to try, and here it is:

Function AvgDaysInMonth()
  AvgDaysInMonth = 365.25 / 12
End Function

Function DaysInMonth(Month As Integer, Year As Integer)
  DaysInMonth = DateSerial(Year, Month + 1, 1) - DateSerial(Year, Month, 1)
End Function

Function IsLeapYear(Year As Integer)
  If DaysInMonth(2, Year) = 29 Then
    IsLeapYear = True
    IsLeapYear = False
  End If
End Function

Function FindNextLeapYear(Year As Integer)
  For Counter = 0 To 3
    If IsLeapYear(Year + Counter) = True Then
      FindNextLeapYear = Year + Counter
    End If
  Next Counter
End Function

Written by Jacob Walker

February 6th, 2015 at 3:10 pm

Posted in Uncategorized