AVERAGE formula VBA

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

I am having a bit of trouble with my code. I am using the average function to find the average of the contents in a set of cells in a column. The function in the cell that contains the average function is =AVERAGE($H$5:$H$200).

When I run my code periodically I add a new row and a new number comes in at the top. When I get the new row of data the AVERAGE function in the cell described above changes to =AVERAGE($H$6:$H$201). I would like to continue to find the average for the most recent set of cells and not have the cells shift on me. I thought the absolute reference would accomplish this...apparently not. Any advice is appreciated.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I see no Vba code here. Any time your using Vba code please always show it to us.
 
Upvote 0
I see no Vba code here. Any time your using Vba code please always show it to us.


The cells with the AVERAGE Function are not included in the VBA. I was assuming they would adjust with the addition of new material.

Code:
Range("B5").Select    ActiveSheet.Paste
    Range("L6").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.AutoFill Destination:=Range("L6:AR5"), Type:=xlFillDefault

Now that I think about it I might need to run a loop with the function I need for each page, or something.

Im afraid the code isn't too helpful.
 
Upvote 0
If your looking for a Vba solution to average all the cells in say column "C"
And put that value in cell "A1" use this script:

Code:
Sub My_Average()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
Cells(1, 1).Value = WorksheetFunction.Average(Range(Cells(1, 3), Cells(lastrow, 3)))
End Sub
 
Upvote 0
If your looking for a Vba solution to average all the cells in say column "C"
And put that value in cell "A1" use this script:

Code:
Sub My_Average()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
Cells(1, 1).Value = WorksheetFunction.Average(Range(Cells(1, 3), Cells(lastrow, 3)))
End Sub


Thanks for the help. I think I figured it out!
 
Upvote 0

Forum statistics

Threads
1,222,098
Messages
6,163,927
Members
451,866
Latest member
cradd64

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