S
Saxman
Hi John,
Am Sat, 30 Aug 2014 08:42:19 +0100 schrieb Saxman:
try:
Sub myAvg()
Dim LRow As Long, i As Long, n As Long
Dim Start As Long
Dim rngC As Range
Dim dblAvg As Double
Dim strCol As String, arrCol As Variant
strCol = "X,Z,AA,AB,AC,AD,AE,AK,AL,AM,AN,AO,AP,AQ,AX"
arrCol = Split(strCol, ",")
With ActiveSheet
For n = LBound(arrCol) To UBound(arrCol)
LRow = .Cells(Rows.Count, arrCol(n)).End(xlUp).Row
.Range(.Cells(2, arrCol(n)), .Cells(LRow, arrCol(n))) _
.Replace what:="-", replacement:=""
.Cells(LRow + 1, arrCol(n)) = "End"
Start = 2
For i = Start To LRow
If .Cells(i + 1, arrCol(n)) > .Cells(i, arrCol(n)) Then
dblAvg = WorksheetFunction.AverageIf(.Range(.Cells(Start,
arrCol(n)), _
.Cells(i, arrCol(n))), ">0", .Range(.Cells(Start,
arrCol(n)), _
.Cells(i, arrCol(n))))
.Range(.Cells(Start, arrCol(n)), .Cells(i,
arrCol(n))).Replace _
what:=0, replacement:=dblAvg, lookat:=xlWhole
.Range(.Cells(Start, arrCol(n)), .Cells(i,
arrCol(n))).Replace _
what:="", replacement:=dblAvg, lookat:=xlWhole
Start = i + 1
End If
Next
.Range(.Cells(2, arrCol(n)), .Cells(LRow, arrCol(n))).NumberFormat
= "0"
Next
End With
End Sub
I ran the above and got a syntax error. On second thoughts it would not
be possible to do multiple columns, as the data needs to be sorted
beforehand. This is so the next sequence can be identified. As you can
see from a sample below, all the columns are random. 'Time' is the
identifier.
Time TS TS1 TS2 TS3 TS4 TS5 TS6
4.45 79 21 41 55 56 79 30
4.45 57 69 69 41 21 45 57
4.45 64 43 68 64 64 32 43
4.45 68 67 49 59 58 - 68
4.45 72 70 57 71 51 9 31
4.45 75 66 57 23 74 63 54
4.45 68 - 51 56 55 67 37
4.45 62 - 22 28 61 55 12
5.15 47 - 35 - 47 31 51
5.15 46 44 51 30 29 46 -
5.15 47 - 19 31 47 21 19
5.15 47 37 46 - 36 28 47
5.15 59 - 23 23 - 32 59
5.15 46 - 34 46 27 38 -
5.15 49 20 49 34 - 18 38
5.15 53 57 44 53 21 45 27
5.15 34 - - - 34 - -
5.45 21 - - 12 21 6 63
5.45 60 54 - - 60 52 54
5.45 39 - - - 25 39 23
5.45 63 17 23 59 53 47 63
5.45 53 41 53 16 28 45 35
5.45 62 27 48 53 28 - 62
5.45 52 30 - - 25 -
5.45 60 27 60 - 45 - -
5.45 56 - 25 25 - 56 -
6.15 86 10 37 58 74 74 86
6.15 25 - - - - - 25
6.15 78 - 78 50 - 58 63
6.15 65 - - - - - 65
6.15 70 - - - 59 56 70
6.15 - - - - - -
6.45 60 39 48 60 32 - 46
6.45 62 57 50 62 39 66 50
6.45 50 52 45 50 27 28 37
6.45 56 36 56 27 30 36 37
6.45 - - 18 24 28 -
6.45 42 22 46 42 - 18 29
6.45 - - - - - 25
6.45 56 27 38 56 31 43 47
6.45 47 - 31 47 42 - -
6.45 62 24 62 21 29 - 29
6.45 52 41 58 47 35 47 23
6.45 - - - 41 - -
7.15 48 20 39 61 48 61 48
7.15 63 54 55 55 56 63 52
7.15 53 35 - 58 53 51 28
7.15 31 - - 31 11 12 -
7.15 46 21 22 12 30 46 34
7.15 30 - - 23 30 10 29
7.15 11 - - 11 11 38 21
7.15 - - - - - -
7.45 81 55 - - - 22 21
7.45 44 12 - 42 - 44 14
7.45 20 33 - 53 10 - 20
7.45 51 60 62 47 37 51 -
7.45 44 57 40 - 19 23 44
7.45 46 50 20 27 39 - 46
7.45 23 38 - - - - 23
7.45 51 70 55 - 47 50 51