I have an excel sheet (examplary extract below) and would like to achieve following with my VBA code (I am a total newbie to VBA).
- Go through all rows in coloumn A
- Calculate Average value for range in column E with identical dates (column A)
- generate a new table with rows: dates; col: average values and create table in a new worksheet.
Important Note:
- Ranges can have different number of rows depending on the number of values (E) for a date (A)
As a starting point I have following VBA Code:
1 Sub GotoNewState()
3 Dim i As Integer
4 Dim startRange As Integer
5 i = 0
7 Do
8 i = i + 1
9 If Cells(ActiveCell.Row + i, ActiveCell.Column).Value <> ActiveCell.Value Then
10 Cells(ActiveCell.Row + i, ActiveCell.Column).Select
11 startRange = -(i - 1)
12 'MsgBox startRange
13 ActiveCell.Offset(-1, 5).FormulaR1C1 = "=AVERAGE("R[" & startRange & "]C[-1]":RC[-1])"
14 Exit Do
15 End If
16 Loop
18 End Sub
However I have problems with the syntax for R1C1 notation as the argument I want to pass to R[argument] returns an error.
I would be happy about other solutions to my described intention above.
Extract from Excel:
A B C D E
13.03.2015 1 300 5.00 0
13.03.2015 2 300 5.00 40
13.03.2015 3 300 5.00 4
13.03.2015 4 300 5.00 2
13.03.2015 5 300 5.00 2
13.03.2015 6 300 5.00 22
20.03.2015 6 300 5.00 0
20.03.2015 5 300 5.00 14
20.03.2015 1 300 5.00 1
20.03.2015 2 300 5.00 0
20.03.2015 3 300 5.00 0
20.03.2015 4 300 5.00 0
27.03.2015 3 300 5.00 0
27.03.2015 4 300 5.00 3
27.03.2015 2 300 5.00 15
27.03.2015 6 300 5.00 147
27.03.2015 5 300 5.00 14
27.03.2015 1 300 5.00 0
02.04.2015 1 300 5.00 8
02.04.2015 2 300 5.00 0
02.04.2015 3 300 5.00 63
02.04.2015 4 300 5.00 0
02.04.2015 5 300 5.00 0
02.04.2015 6 300 5.00 3
17.04.2015 1 300 5.00 7
17.04.2015 2 300 5.00 1
17.04.2015 3 300 5.00 19
17.04.2015 4 300 5.00 0
17.04.2015 5 300 5.00 159
17.04.2015 6 300 5.00 84
30.04.2015 1 300 5.00 0
30.04.2015 2 300 5.00 0
30.04.2015 3 300 5.00 2
30.04.2015 3 300 5.00 2
30.04.2015 4 300 5.00 0
30.04.2015 5 300 5.00 182
30.04.2015 6 300 5.00 2
...