strangebiscuit
New Member
- Joined
- Nov 25, 2013
- Messages
- 35
I've got worksheets that have information about a different persons utility usage on each row. Column A has the name, Column B has an ID, Column C has the type of utility (GAS or POWER), and D has their usage (just a number).
I need a macro to go through each sheet in the workbook and put separates averages for GAS and POWER usage on each sheet.
I've got something that works by simply looping through the rows and using IF statements to add to counters based on whether Column C lists GAS or POWER.
But I was wondering if there's a better way to do it using formulas or something so that the averages will update if the values are changed in any of the rows. I looked into the AVERAGEIF function but couldn't seem to make anything work. Does anyone have any ideas?
A sample sheet looks like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Name[/TD]
[TD]ID[/TD]
[TD]Utility[/TD]
[TD]Usage[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][TABLE="width: 144"]
<tbody>[TR]
[TD="width: 144"]Jamie Doe[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]43334[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]654[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Jimmie Doe[/TD]
[TD]22344[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]233[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]John Doe[/TD]
[TD]12123[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]POWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]6553[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Harry Doe[/TD]
[TD]1456444[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]156[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Barry Doe[/TD]
[TD]42344[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]POWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]954[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Willy Doe[/TD]
[TD]212123[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]POWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]3000[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Tabatha Doe[/TD]
[TD]135235[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]487[/TD]
[/TR]
</tbody>[/TABLE]
My macro code (not utilizing functions) is the following:
I feel like there must be a better, simpler and probably obvious solution using functions but I can't seem to puzzle it out. If anyone could advise me or just tell me if I'm being an idiot, I'd be very appreciative.
Thanks very much!
I need a macro to go through each sheet in the workbook and put separates averages for GAS and POWER usage on each sheet.
I've got something that works by simply looping through the rows and using IF statements to add to counters based on whether Column C lists GAS or POWER.
But I was wondering if there's a better way to do it using formulas or something so that the averages will update if the values are changed in any of the rows. I looked into the AVERAGEIF function but couldn't seem to make anything work. Does anyone have any ideas?
A sample sheet looks like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Name[/TD]
[TD]ID[/TD]
[TD]Utility[/TD]
[TD]Usage[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][TABLE="width: 144"]
<tbody>[TR]
[TD="width: 144"]Jamie Doe[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]43334[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]654[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Jimmie Doe[/TD]
[TD]22344[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]233[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]John Doe[/TD]
[TD]12123[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]POWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]6553[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Harry Doe[/TD]
[TD]1456444[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]156[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Barry Doe[/TD]
[TD]42344[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]POWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]954[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Willy Doe[/TD]
[TD]212123[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]POWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]3000[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Tabatha Doe[/TD]
[TD]135235[/TD]
[TD][TABLE="width: 53"]
<tbody>[TR]
[TD="width: 53"]GAS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD="align: right"]487[/TD]
[/TR]
</tbody>[/TABLE]
My macro code (not utilizing functions) is the following:
Code:
Option Explicit
Sub GetScoreAverage()
Dim Current As Worksheet
' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
Dim N As Long, i As Long
Dim GasCount As Long, GasTotal As Double, PowCount As Long, PowTotal As Double
N = Current.Cells(Current.Rows.count, "D").End(xlUp).Row
'Loop through rows on current sheet and count GAS and POWER totals
For i = 2 To N
If Current.Cells(i, "C").Value = "GAS" Then
GasCount = GasCount + 1
GasTotal = GasTotal + Current.Cells(i, "D").Value
End If
If Current.Cells(i, "C").Value = "POWER" Then
PowCount = PowCount + 1
PowTotal = PowTotal + Current.Cells(i, "D").Value
End If
Next i
'Place averages for both types at the bottom of the sheet
With Current.Cells(Current.Cells.Find(What:="*", AFter:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 2, 1)
.Resize(2).Value = Application.Transpose(Array("Average Gas", "Average Power"))
.Resize(2).Font.Bold = True
' Average Gas Use Formula
.Offset(, 1).FormulaR1C1 = GasTotal / GasCount
' Average Power Use Formula
.Offset(1, 1).FormulaR1C1 = PowTotal / PowCount
End With
'Reset Variables
N = 0
i = 0
GasCount = 0
GasTotal = 0
PowCount = 0
PowTotal = 0
Next
'Display a message indicating that the macro has finished
MsgBox "Completed...", vbInformation
End Sub
I feel like there must be a better, simpler and probably obvious solution using functions but I can't seem to puzzle it out. If anyone could advise me or just tell me if I'm being an idiot, I'd be very appreciative.
Thanks very much!