Você está na página 1de 7

Public Sub CalculateTotals()

Dim totEnergy As Long

Dim count As Integer

Dim year As Integer

Dim month As Integer

Dim eCost As Double

Dim gCost As Double

Dim aCost As Double

Dim wCost As Double

Dim fCost As Double

Dim oCost As Double

Dim eRate As Double

Dim wTreated As Double

Dim eUse As Long

Dim gUse As Long

Dim rowNum As Integer

Dim mStart As Integer

Dim mEnd As Integer

Dim yStart As Integer

Dim sRow As Integer

totalEnergy = 0

count = 0

rowNum = Range("monthSortEnd").Row

Application.ScreenUpdating = False

Application.EnableEvents = False

ThisWorkbook.Sheets("General Information").Activate

Sheets("General Information").Range("GIHidden").EntireRow.Hidden = False

mStart = Range("monthByMonthData").Row
mEnd = Range("monthByMonthDataEnd").Row

Range("monthByMonthData").Select

Selection.Sort Key1:=Range("B" & mStart), Order1:=xlDescending, Key2:=Range( _

"C" & mStart), Order2:=xlDescending, Key3:=Range("D" & mStart), Order3:=xlDescending _

, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:= _

xlTopToBottom, DataOption1:=xlSortTextAsNumbers, DataOption2:=xlSortNormal _

, DataOption3:=xlSortNormal

'clear all the values before refreshing. cols D, I have calcs so do not clear. these will be cleared
automatically.

sRow = Range("monthSortEnd").Row

Range("B" & sRow - 11 & ":C" & sRow).Select

Selection.ClearContents

Range("E" & sRow - 11 & ":H" & sRow).Select

Selection.ClearContents

Range("J" & sRow - 11 & ":J" & sRow).Select

Selection.ClearContents

Range("L" & sRow - 11 & ":S" & sRow).Select

Selection.ClearContents

sRow = Range("yearSortEnd").Row

Range("B" & sRow - 3 & ":H" & sRow).Select

Selection.ClearContents

Range("totEnergy").Select

Selection.ClearContents

'calc electric values

For Each c In Range("D" & mStart & ":" & "D" & mEnd)

If count < 12 And Not IsNull(c.Value) And Not c.Value = 0 Then 'check for null energy

If Not IsNull(c.Offset(0, 1).Value) And Not c.Offset(0, 1).Value = 0 Then ' then check for null
cost
c.Select

totalEnergy = totalEnergy + c.Value

year = ActiveCell.Offset(0, -2).Value

month = ActiveCell.Offset(0, -1).Value

eCost = ActiveCell.Offset(0, 1).Value

wTreated = ActiveCell.Offset(0, 3).Value

eRate = ActiveCell.Offset(0, 4).Value

eUse = c.Value

Range("B" & rowNum) = year

Range("C" & rowNum) = month

Range("E" & rowNum) = eUse

Range("G" & rowNum) = wTreated

Range("H" & rowNum) = eCost

'Range("K" & rowNum) = eRate

count = count + 1

rowNum = rowNum - 1 'put the latest values last to avoid another sort for graph axis.

End If

End If

Next

'below 4 categories were split individually cos the values would not be taken from the latest
12months but would fill in from previous data.

'calc N.Gas values

count = 0

rowNum = Range("monthSortEnd").Row

For Each c In Range("F" & mStart & ":" & "F" & mEnd)

If count < 12 And Not IsNull(c.Value) And Not c.Value = 0 Then 'check for null energy

If Not IsNull(c.Offset(0, 3).Value) And Not c.Offset(0, 3).Value = 0 Then ' then check for null
cost

c.Select
Range("F" & rowNum) = c.Value

Range("J" & rowNum) = ActiveCell.Offset(0, 3).Value

count = count + 1

rowNum = rowNum - 1

End If

End If

Next

'calc fuel 2 values

count = 0

rowNum = Range("monthSortEnd").Row

For Each c In Range("J" & mStart & ":" & "J" & mEnd)

If count < 12 And Not IsNull(c.Value) And Not c.Value = 0 Then 'check for null energy

If Not IsNull(c.Offset(0, 1).Value) And Not c.Offset(0, 1).Value = 0 Then ' then check for null
cost

c.Select

Range("L" & rowNum) = c.Value

Range("M" & rowNum) = ActiveCell.Offset(0, 1).Value

count = count + 1

rowNum = rowNum - 1

End If

End If

Next

'calc alternative energy values

count = 0

rowNum = Range("monthSortEnd").Row

For Each c In Range("L" & mStart & ":" & "L" & mEnd)

If count < 12 And Not IsNull(c.Value) And Not c.Value = 0 Then 'check for null energy
If Not IsNull(c.Offset(0, 1).Value) And Not c.Offset(0, 1).Value = 0 Then ' then check for null
cost

c.Select

Range("N" & rowNum) = c.Value

Range("O" & rowNum) = ActiveCell.Offset(0, 1).Value

count = count + 1

rowNum = rowNum - 1

End If

End If

Next

'calc other energy values

count = 0

rowNum = Range("monthSortEnd").Row

For Each c In Range("N" & mStart & ":" & "N" & mEnd)

If count < 12 And Not IsNull(c.Value) And Not c.Value = 0 Then 'check for null energy

If Not IsNull(c.Offset(0, 1).Value) And Not c.Offset(0, 1).Value = 0 Then ' then check for null
cost

c.Select

Range("P" & rowNum) = c.Value

Range("Q" & rowNum) = ActiveCell.Offset(0, 1).Value

count = count + 1

rowNum = rowNum - 1

End If

End If

Next

'calc water & sewer values

count = 0

rowNum = Range("monthSortEnd").Row
For Each c In Range("P" & mStart & ":" & "P" & mEnd)

If count < 12 And Not IsNull(c.Value) And Not c.Value = 0 Then 'check for null energy

If Not IsNull(c.Offset(0, 1).Value) And Not c.Offset(0, 1).Value = 0 Then ' then check for null
cost

c.Select

Range("R" & rowNum) = c.Value

Range("S" & rowNum) = ActiveCell.Offset(0, 1).Value

count = count + 1

rowNum = rowNum - 1

End If

End If

Next

Range("totEnergy").Value = totalEnergy

rowNum = Range("yearSortStart").Row

yStart = Range("yearByYearData").Row

Range("yearByYearData").Select

Selection.Sort Key1:=Range("B" & yStart), Order1:=xlAscending, _

Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:= _

xlTopToBottom, DataOption1:=xlSortTextAsNumbers

For Each c In Range("C" & yStart & ":" & "C" & yStart + 3) 'leave out the latest since it appear in the
last 12 months(hardcoded on sheet)

c.Select

year = ActiveCell.Offset(0, -1).Value

eCost = ActiveCell.Offset(0, 4).Value

gCost = ActiveCell.Offset(0, 2).Value

aCost = ActiveCell.Offset(0, 6).Value

wCost = ActiveCell.Offset(0, 7).Value


fCost = ActiveCell.Offset(0, 8).Value

oCost = ActiveCell.Offset(0, 9).Value

wTreated = ActiveCell.Offset(0, 3).Value

gUse = ActiveCell.Offset(0, 1).Value

totalEnergy = ActiveCell.Offset(0, 5).Value

eUse = c.Value

Range("B" & rowNum) = year

Range("C" & rowNum) = eUse

Range("D" & rowNum) = gUse

Range("E" & rowNum) = gCost

Range("F" & rowNum) = wTreated

Range("G" & rowNum) = eCost

Range("H" & rowNum) = totalEnergy

Range("I" & rowNum) = aCost

Range("J" & rowNum) = wCost

Range("K" & rowNum) = fCost

Range("L" & rowNum) = oCost

rowNum = rowNum + 1 'put the earliest values first to avoid another sort for graph axis.

Next

Sheets("General Information").Range("GIDefault").Select

Sheets("General Information").Range("GIHidden").EntireRow.Hidden = True

Application.EnableEvents = True

Application.ScreenUpdating = True

End Sub

Você também pode gostar