Monthly Averages

From IARC 207 Wiki
Revision as of 11:47, 28 November 2011 by 137.229.29.95 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Use this program to compute monthly averages.

Option Explicit
Public Sub monthly_avg()
  Dim datecol As Integer
  Dim inputcol As Integer
  Dim outcol As Integer
  Dim worksheetz As Integer
  Dim startrow As Long
  Dim outputdate As Boolean
  Dim outputcount As Boolean
  Dim columnincr As Integer
  
  ' stuff that should be set just initially
  datecol = 1
  worksheetz = 3
  startrow = 4
  outputcount = True
  outputdate = True
  inputcol = 2
  outcol = 30
  Call avg_column(datecol, inputcol, outcol, worksheetz, startrow, outputdate, outputcount)
  
End Sub

Public Sub avg_column(datecol As Integer, inputcol As Integer, outcol As Integer, worksheetz As Integer, startrow As Long, outputdate As Boolean, outputcount As Boolean)
  Dim row As Long
  Dim count As Integer
  Dim themonth As Integer
  Dim curdata As Double
  Dim monthsum As Double
  Dim outrow As Integer
  
  outrow = startrow
  row = startrow
 
  themonth = Int(Format(Worksheets(worksheetz).Cells(row, datecol).Value, "MM"))
  curdata = Worksheets(worksheetz).Cells(row, inputcol).Value
  If Abs(curdata) < 6999 Then
    monthsum = curdata
    count = 1
  End If
  row = row + 1
  Do While Worksheets(worksheetz).Cells(row, datecol).Value <> ""
    If themonth = Int(Format(Worksheets(worksheetz).Cells(row, datecol).Value, "MM")) Then
      ' same day
      curdata = Worksheets(worksheetz).Cells(row, inputcol).Value
      If Abs(curdata) < 6999 Then
        monthsum = monthsum + curdata
        count = count + 1
      End If
    Else:
      ' new day, do some outputting
      If outputdate = True Then
        ' output the day
        Worksheets(worksheetz).Cells(outrow, outcol).Value = themonth
        If count > 0 Then
          ' output daily average
          Worksheets(worksheetz).Cells(outrow, outcol + 1).Value = monthsum / count
        End If
        If outputcount = True Then
          ' output the count if the boolean is true
          Worksheets(worksheetz).Cells(outrow, outcol + 2).Value = count
        End If
      Else:
        ' don't output the day
        If count > 0 Then
          ' output daily average
          Worksheets(worksheetz).Cells(outrow, outcol).Value = monthsum / count
        End If
        If outputcount = True Then
          ' output the count if the boolean is true
          Worksheets(worksheetz).Cells(outrow, outcol + 1).Value = count
        End If
      End If
      outrow = outrow + 1
      ' okay, done outputting.  Now to ingest the new data as before.
      ' first reset the vars.
      count = 0
      monthsum = 0
      themonth = Int(Format(Worksheets(worksheetz).Cells(row, datecol).Value, "MM"))
      curdata = Worksheets(worksheetz).Cells(row, inputcol).Value
      If Abs(curdata) < 6999 Then
        monthsum = curdata
        count = 1
      End If
      
    End If
    row = row + 1
  Loop
    ' drop in the stuff for the last day of year.
    ' new day, do some outputting
    If outputdate = True Then
     ' output the day
     Worksheets(worksheetz).Cells(outrow, outcol).Value = themonth
     If count > 0 Then
       ' output daily average
       Worksheets(worksheetz).Cells(outrow, outcol + 1).Value = monthsum / count
     End If
     If outputcount = True Then
       ' output the count if the boolean is true
       Worksheets(worksheetz).Cells(outrow, outcol + 2).Value = count
     End If
   Else:
     ' don't output the day
     If count > 0 Then
       ' output daily average
       Worksheets(worksheetz).Cells(outrow, outcol).Value = monthsum / count
     End If
     If outputcount = True Then
       ' output the count if the boolean is true
       Worksheets(worksheetz).Cells(outrow, outcol + 1).Value = count
     End If
   End If
End Sub