Average a range of cells in VBA

jojasary

New Member
Joined
Dec 7, 2007
Messages
9
OK this might sound simple, but how can I make an average, in VBA of a range of cells?

I want to average cells b2 to b24

thanks
 
The following code finds today's date in row 7, and calculates the average in row 11 from column D to the column containing today's date.
It is assumed that cells containing 0 should be included in the average.
Code:
Sub Avg()
Dim col As Variant, myresult!
col = Application.Match(CLng(Date), Rows(7), 0)
If Not IsError(col) Then
    myresult = Application.Average(Range([D11], Cells(11, col)))
Else
    MsgBox "Today's date not found in row 11"
    Exit Sub
End If
End Sub
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The following code finds today's date in row 7, and calculates the average in row 11 from column D to the column containing today's date.
It is assumed that cells containing 0 should be included in the average.
Code:
Sub Avg()
Dim col As Variant, myresult!
col = Application.Match(CLng(Date), Rows(7), 0)
If Not IsError(col) Then
    myresult = Application.Average(Range([D11], Cells(11, col)))
Else
    MsgBox "Today's date not found in row 11"
    Exit Sub
End If
End Sub

That was great!! Thanks

But let me explain better....

I have this Table in a worksheet and i need to make an average in "b3" and "b5" the range change daily. the info located after today in those rows need to be diferent like this exapmle

ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
127/08/201828/06/201829/06/201830/06/201801/07/201802/07/201803/07/201804/07/201805/07/201806/07/201807/07/201808/07/201809/07/201810/07/201811/07/201812/07/201813/07/201814/07/201815/07/201816/07/201817/07/201818/07/201819/07/201820/07/201821/07/201822/07/201823/07/201824/07/201825/07/201826/07/201827/07/201828/07/201829/07/201830/07/2018
2Total Pool577564570575560554580596604597615620645610580567577
3Estimated Daily input=dynamic range average from D3 to "today"=D4*D5=E4*E5156172180180164180156180168164180180180180=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3=b3
4Productivity Quote x Tech44444444444444444444444444444444444
5Number of Techs=dynamic range average from D3 to "today"45443943454541403945424145454545=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5=d5

<tbody>
</tbody>

Thanks for help...
 
Upvote 0
I thought you wanted a VBA solution.

In B3 :
=AVERAGE(D3:OFFSET(D3,0,MATCH(TODAY(),1:1,0)-3))

In B5 :
=AVERAGE(D5:OFFSET(D5,0,MATCH(TODAY(),1:1,0)-3))

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
footoo, thanks it was work !! only i replace the part of my code qith this formula and the macro wroks great! thanks again
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,245
Members
448,952
Latest member
kjurney

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