View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Greg Wilson[_4_] Greg Wilson[_4_] is offline
external usenet poster
 
Posts: 218
Default Automatically substituting missing data

This was rather rushed but I think it does what you want.

Note that there is a potential complication not accounted
for, specifically: If a period of missing data exceeds
the gap between the next period of missing data then the
forward averaging will include blank cells thus returning
an inappropriate result. This can be corrected for but it
will make the code substantially more complicated. Your
description didn't take this into account so I assume it's
not very likely. If you need this corrected for you'll
have to specify the rules.

You may have to correct for wordwrap corruption of the
code.

Assumptions:
1) Hourly data is in Column A
2) Raw data is in Column B
3) Corrected data is to be placed in Column C

Sub CorrectData()
Dim BlankRng As Range, AvgRng As Range
Dim Rng As Range, C As Range, CC As Range
Dim Rw As Long, i As Integer
Dim Avg As Single
i = 0
Rw = Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range(Cells(1, 2), Cells(Rw, 2))
For Each C In Rng
If C = "" Then
i = i + 1
ElseIf i 0 Then
C.Offset(, 1) = C
Set AvgRng = Range(Range(C.Offset(-i - 1), _
C.Offset(-2 * i)), Range(C, C.Offset(i - 1)))
Avg = Application.Average(AvgRng)
Set BlankRng = Range(C.Offset(-i), C.Offset(-1))
For Each CC In BlankRng
CC.Offset(, 1) = Avg
Next
i = 0
Else
C.Offset(, 1) = C
End If
Next

End Sub

Be advised I'm just a VBA student.

Regards,
Greg