View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Rick Hansen Rick Hansen is offline
external usenet poster
 
Posts: 104
Default Average Calculation in Excel VB

Hey D, Here is One other thing I was playing with last night with your
project. This macro routine will now sort the data in Columns A & B (set
them up like your data). So new data items can be placed at the bottom of
the list or anywhere in the list. So when this new macro is executed it
Sorts all the data, then break down group and applies the average formula
for each group as the old macro. Just thought I pass this on to ya...

Thanks again for the good words, Rick

=======================================
Sub GroupSortAvg()
Dim LastRow As Long, lRow As Long
Dim ws1 As Worksheet
Dim Top As Long
Dim ZoneStr As String
Dim sRng As Range


Set ws1 = Worksheets("sheet1")
With ws1
LastRow = .Range("A3").End(xlDown).Row
Set sRng = .Range("A3:B" & LastRow)

'' sort first then, group average
sRng.Sort Key1:=Range("A3"), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom

lRow = 3 '' <- start row
Do
Top = lRow '' save top of range position
ZoneStr = Left(.Cells(Top, "A"), 2) '' ZoneCode string
Do '' loop thru like zonecode string
lRow = lRow + 1 '' to find last postion of like zone code
Loop While (ZoneStr = Left(.Cells(lRow, "A"), 2)) '' this give bottom
of range (lrow-1)

Range("C" & Top).Formula = "=Average(B" & Top & ":B" & lRow - 1 & ")"
Loop While (lRow < LastRow + 1)
End With

End Sub
===========================

"Dastard" wrote in
message ...

Its awesome .. its running just the way i wanted it to run .. thanks a
lot rick :D
:)
Thank you very much


--
Dastard
------------------------------------------------------------------------
Dastard's Profile:

http://www.excelforum.com/member.php...o&userid=34406
View this thread: http://www.excelforum.com/showthread...hreadid=541843