Fill blanks in a range with the average of that range

Jo4x4

Board Regular
Joined
Jan 8, 2011
Messages
136
Hi everybody,

I have a small range A1 to A10 which I fill with data. However, sometimes there may be two or three of the cells that has no value. I would like to fill these empty cells with the average of the cells that did have values.

Thanks
Jo

Win XP, Office 2010
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
Sub FillBlankAverage()
    Dim x As Integer
    Dim cnt As Single
    Dim sum As Single
    Dim Avg As Single
    For x = 1 To 10
        If Cells(x, 1) <> "" Then
            cnt = cnt + 1
            sum = sum + Cells(x, 1)
        End If
    Next
    Avg = sum / cnt
    For x = 1 To 10
        If Cells(x, 1) = "" Then Cells(x, 1) = Avg
    Next
End Sub
 
Upvote 0
This non-looping method should be more efficient, though if only 10 cells it wouldn't matter.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> FillAvg()<br>    <SPAN style="color:#00007F">Dim</SPAN> Avg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN><br>    <br>    <SPAN style="color:#00007F">Const</SPAN> myRng <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "A1:A10" <SPAN style="color:#007F00">'<-- Change to suit</SPAN><br>    Avg = WorksheetFunction.Average(Range(myRng))<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    Range(myRng).SpecialCells(xlCellTypeBlanks).Value = Avg<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Hi Peter,
Thank you, but if cell A9 and A10 does not have values, it does not fill them, it only fills the other blanks?

Jo
 
Upvote 0
Hi Peter,
Thank you, but if cell A9 and A10 does not have values, it does not fill them, it only fills the other blanks?

Jo
It does depend on what else is, or could be, in your sheet but you are correct there are circumstances that would trigger that behaviour. If you wanted to pursue this method for your sheet, I'd want to know a bit more about the sheet and what is happening on it. For example, is there ever anything in A11?

To see the effect, put something in your sheet anywhere below row 10 (eg F15) and run the code again.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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