I have a spreadsheet that populates itself from individual stock data downloaded and subsequently calculated.
The data is sorted per Sector Category - each sector may have one or more than one hundred tickers within it.
With the excellent help from those on this forum I have been able to get the logic that calculates a sector average return, and I would like to rank individual values within each sector. I just can't get my head around the logic inside the routine - I would be willing to call another routine if that works.
Here is the code commented for my benefit:
Any ideas? The Rank() function has me stumped with all these variable counters and offsets.
Thanks in advance.
The data is sorted per Sector Category - each sector may have one or more than one hundred tickers within it.
With the excellent help from those on this forum I have been able to get the logic that calculates a sector average return, and I would like to rank individual values within each sector. I just can't get my head around the logic inside the routine - I would be willing to call another routine if that works.
Here is the code commented for my benefit:
Code:
Sub AvgSectors7Day()
Dim Count As Integer
Dim mySum As Double
Dim CellPosition As Range
Dim J As Integer
Dim K As Integer
SortOnSectorField
Set CellPosition = Range("D4") 'Sector Name
Do Until CellPosition.value = ""
Count = Count + 1
mySum = mySum + CellPosition.Offset(, 9) '7 Day Return Column
IF CellPosition.Offset(1) <> CellPosition Then 'If one row down <> this one
CellPosition.Offset(, 25) = mySum / Count 'Calculate the Average
K = Count - 1 'Decr Count by 1 due to Loop
For J = 1 To K 'Loop to copy the Avg to all rows in Sector
CellPosition.Offset(-J, 25) = mySum / Count 'It copies UP
Next J
'
'Routine to Calculate Rank() in Sector here?
'
Count = 0 'Out Of the IF Loop, reset the counters
mySum = 0
End IF
Set CellPosition = CellPosition.Offset(1) 'Next Row to Test
Loop
End Sub
Any ideas? The Rank() function has me stumped with all these variable counters and offsets.
Thanks in advance.