Populate blank cells with average of nearest non-blank values

chibibenz

New Member
Joined
Jul 2, 2019
Messages
12
Hello,

I am looking for a formula or macro to help me populate blank cells. The situation is that I have minute-by-minute data, but there are some missing minutes. Sometimes a single minute is missing, sometimes a chunk of minutes is missing. I have a macro that inserts a row every time there is a missing minute. Where I am stuck is that I need to fill in data for the missing minutes with the average of the nearest non-blank before and after.

For a single minute of missing data, the formula is very easy. I can just enter =AVERAGE(R[-1]C,R[1]C).
For two minutes of missing data, the first minute will have the formula =AVERAGE(R[-1]C,R[2]C), and the second minute of missing data has the formula =AVERAGE(R[-2]C,R[1]C). (and so on).

I have a TON of missing minutes to populate, and the fact that I can't just make a formula and drag it down across all the missing rows is making this very time consuming. Can you think of a way to write the formula that would automate this? I'm thinking maybe some kind of indirect function but I'm just not sure. I'm open to adding helper columns, anything to speed up the process of populating all these empty rows! Thank you!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Maybe something like this:
VBA Code:
Sub a1125754a()
'https://www.mrexcel.com/board/threads/populate-blank-cells-with-average-of-nearest-non-blank-values.1125754/
Dim i As Long, j As Long, n As Long
Application.ScreenUpdating = False
n = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To n - 1
    If Cells(i, "A") = "" Then
        j = i
        Do While Cells(i, "A") = ""
            i = i + 1
        Loop
        Range(Cells(j, "A"), Cells(i - 1, "A")) = (Cells(j - 1, "A") + Cells(i, "A")) / 2
    End If
Next

Application.ScreenUpdating = True
End Sub

Example:
Book1 (version 1).xlsb
A
1data
21
3
43
5
6
7
8
95
107
11
12
1310
Sheet1


Result:
Book1 (version 1).xlsb
A
1data
21
32
43
54
64
74
84
95
107
118.5
128.5
1310
Sheet1
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top