Doorgaan naar hoofdcontent

Excel: nice example of the formula EOMONTH

Got a question about calculating an average for monthly data. Here is an example of the data starting in cell A1:

date dieselprice avgpermonth
1-1-2007 0,985 0,96355
2-1-2007 0,985 0,96355
3-1-2007 0,984 0,96355
4-1-2007 0,984 0,96355
5-1-2007 0,978 0,96355
6-1-2007 0,964 0,96355
7-1-2007 0,964 0,96355
8-1-2007 0,964 0,96355
9-1-2007 0,964 0,96355
10-1-2007 0,964 0,96355
11-1-2007 0,964 0,96355
12-1-2007 0,958 0,96355
13-1-2007 0,958 0,96355
14-1-2007 0,958 0,96355
15-1-2007 0,958 0,96355
16-1-2007 0,954 0,96355
17-1-2007 0,954 0,96355
18-1-2007 0,984 0,96355
19-1-2007 0,948 0,96355
20-1-2007 0,948 0,96355
21-1-2007 0,948 0,96355
22-1-2007 0,948 0,96355
23-1-2007 0,948 0,96355
24-1-2007 0,958 0,96355
25-1-2007 0,964 0,96355
26-1-2007 0,964 0,96355
27-1-2007 0,964 0,96355
28-1-2007 0,964 0,96355
29-1-2007 0,964 0,96355
30-1-2007 0,964 0,96355
31-1-2007 0,964 0,96355
1-2-2007 0,964 0,98239
2-2-2007 0,964 0,98239
3-2-2007 0,978 0,98239
4-2-2007 0,978 0,98239
5-2-2007 0,978 0,98239
6-2-2007 0,976 0,98239
7-2-2007 0,992 0,98239
8-2-2007 0,992 0,98239
9-2-2007 0,982 0,98239
10-2-2007 0,982 0,98239
11-2-2007 0,982 0,98239
12-2-2007 0,982 0,98239
13-2-2007 0,993 0,98239
14-2-2007 0,983 0,98239
15-2-2007 0,983 0,98239
16-2-2007 0,983 0,98239
17-2-2007 0,973 0,98239
18-2-2007 0,973 0,98239
19-2-2007 0,973 0,98239
20-2-2007 0,974 0,98239
21-2-2007 0,974 0,98239
22-2-2007 0,974 0,98239
23-2-2007 0,984 0,98239
24-2-2007 0,994 0,98239
25-2-2007 0,994 0,98239
26-2-2007 0,994 0,98239
27-2-2007 1,004 0,98239
28-2-2007 1,004 0,98239

Of course, I could have used a formula like 

=AVERAGE($C$2:$C$32)

and change that every time it is needed. In stead I wanted to use a formula which calculates the average for each month automatically, depending on the row it is in.

I ended up with this one, from cell C2 down:

=AVERAGE(INDIRECT(ADDRESS(ROW()-DAY(A2)+1;2)&":"&ADDRESS(ROW()+DAY(EOMONTH(A2;0))-DAY(A2);2)))

This formula works fine for me. Just wondering wether it can be done more easy.

On a second worksheet I created:

end of month avg per   month
0 31-1-2007 0,964
1 28-2-2007 0,982
2 31-3-2007 1,000
3 30-4-2007 1,024
4 31-5-2007 1,026
5 30-6-2007 1,049
6 31-7-2007 1,058
7 31-8-2007 1,046
8 30-9-2007 1,077
9 31-10-2007 1,091
10 30-11-2007 1,180
11 31-12-2007 1,144
In cell B2 I put the formula:

=EOMONTH(prices!$A$2;A2)

and copied it downwards.

In cell C2 I put the formula:

=VLOOKUP(B2;prices!A:C;3;FALSE)

in order to  look up the right average on the first sheet.

Thanks to +Brian Canes and +Shane Devenshire we can even replace the formula in C2:

=AVERAGEIFS(prices!B$2:B$3660;prices!A$2:A$3660;">"&EOMONTH(B2;-1);prices!A$2:A$3660;"<="&EOMONTH(B2;0))

If we use the last formula we don't even need the third helper column on the first sheet anymore.

You can download the file ExcelExampleEOmonth.xlsx through

https://drive.google.com/folderview?id=0B7HgkOwFZtdZVmhRQUZFM28yc1U&usp=sharing

Reacties

Populaire posts van deze blog

Friesland: nu ruim 1500 Friese familiewapens doorzoekbaar

Inleiding Het totale overzicht is nu ook als PDF te downloaden: rptWapens Al jaren verzamel ik de teksten van grafzerken en andere voorwerpen om ze in een database bijeen te brengen. Op heel veel oude grafzerken, rouwborden of andere voorwerpen in Friesland treffen we naast tekst ook vaak familiewapens aan. Ruim een jaar geleden ben ik gestart met het koppelen van familiewapens aan deze teksten. Met behulp van deze database kunnen we nu steeds meer voorheen onbekende wapens thuisbrengen of andersom, initialen herleiden tot de bijbehorende namen. Een voorbeeld. Zo kennen we uit het boek Fries Zilver nummer 894 met de tekst: TDH RBB 1669 Tjitte D. Wijngaarden geboren 6 october 1874 Daarbij een mannen- en een vrouwenwapen. Mannenwapen blijkt dat van Hoitinga te zijn. De letters blijken vervolgens te herleiden tot Tjepke Douwes Hoitinga en Rienkje Bouwes Bruinsma uit Nijland. Vermoedelijk getrouwd in 1669! Opgelost. Ander voorbeeld. Op een lepel uit het boek Dokk...

Excel: VBA script om wachtwoord te verwijderen

Af en toe krijg ik een vraag om een wachtwoord van een Excel blad te halen. Doodsimpel met VBA. Hier een script dat ik gebruik: Sub WachtwoordCrack()     Dim a As Integer, b As Integer, c As Integer, d As Integer, _     e As Integer, f As Integer, g As Integer, h As Integer, _  I As Integer, j As Integer, k, m As Integer     Dim begin As Date, eind As Date     Dim duur As String     Dim objSheet As Worksheet     begin = TimeValue(Time)     On Error Resume Next     For Each objSheet In Application.Worksheets         For a = 65 To 66: For b = 65 To 66: For c = 65 To 66             For d = 65 To 66: For e = 65 To 66: For f = 65 To 66                 For g = 65 To 66: For h = 65 To 66: For I = 65 To 66                     For j = 65 To 66: For k = 65 To...

Excel: Creating Your Own Tabs With XML And VBA

In order to create you own nice looking tabs you need the Custom UI Editor for Microsoft Office. You can download it for free. How is this different from choosing File , Options , Customize Ribbon and adding a new tab with new groups and then commands to the group? You can also move the new tab to any position. T hen your new tab is always there. Even when you don't open the specific file with the right VBA. In my example the outcome looks like this, a nice tab, placed in front of the Home Tab. You will only get this tab when you start the specific example (which you can download). The XML behind this: <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">    <ribbon startFromScratch="false">     <tabs>       <tab id="MaxIlze" label="Dossier Overzicht" insertBeforeMso="TabHome">         <group id="customGroup1" label="Adressen verrijken">         ...