Why can't is the average function not working?

jaimep

New Member
Joined
Sep 5, 2011
Messages
7
Hi,

I am trying to get an average of a weeks worth of results.

This is what I have tried

Sub PaymentFunnelsAutomate2()
'
Sheets("DATA").Select
Range("a1").Select
Selection.End(xlToRight).Select

Dim c As Range
Set c = ActiveCell

Sheets("DATA").Select
Range("a3").Select
Selection.End(xlToRight).Select

Dim d As Range
Set d = ActiveCell

If c + 13 = d Then
Sheets("DATA").Select
Range("A1").Select
Selection.End(xlToRight).Select
Selection.Offset(0, 1).Select
ActiveCell = c + 7

'need to include the code for adding the weekly figures

Sheets("EM").Select
Range("A31").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, -6).Range("A1:G1").Select
ActiveCell.Activate

Dim e As Range
Set e = ActiveCell

Sheets("Weekly EM %").Select
Range("E31").Select
Selection.End(xlToRight).Select
Selection.Offset(0, 1).Select
ActiveCell = Average(EM!e)

End If

End Sub

But I get a compile error on the last line of code, the Average function

Compile error: Sub or function not defined

Can anybody help to clear up why and maybe offer a solution?

Thanks,
Jaime
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Difficult to tell with all that selecting going on but maybe

Code:
ActiveCell.Value = WorksheetFunction.Average(Sheets("EM").Range(e.Address))
 
Upvote 0
Hi Peter,

Thanks for getting back to me.

Yeah, sorry about the messiness of the code.

I want to choose the last seven days worth of data, all from one line, and then take the average of this data.

This is what I used to select the last 7 days
Sheets("EM").Select
Range("A31").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, -6).Range("A1:G1").Select
ActiveCell.Activate

Dim e As Range
Set e = ActiveCell


I then wanted to go to a different sheet and use this reference (e) to get an average of it. Maybe there is a better way?

Sheets("Weekly EM %").Select
Range("E31").Select
Selection.End(xlToRight).Select
Selection.Offset(0, 1).Select
'ActiveCell.Value = WorksheetFunction.Average(Sheets("Weekly EM %").Range(e))
ActiveCell = Average(EM!e)


I tried to use the code that you sent accross but it said 'application defined or object defined error'.

Unfortunately, I am a bit of a novice so do not really understand this error. Is it something to do with the Address in (e.Address)?

Thanks,
Jaime
 
Upvote 0
Before the line of code that errors try

Code:
MsgBox e.Address

Does that return the range reference that you are expecting?
 
Upvote 0
I have input that bit of code and it is giving me a MsgBox saying $GD$31 which is the first cell of the range that I have selected.

It then goes on to say
'Unable to get the Average of the WorksheetFunction class'.
 
Upvote 0
Actually!!!!

The code you wrote was perfect! I made a mistake when copying it in!!
THANKS ALOT!!!:)
 
Upvote 0
Hi VoG,

Maybe, one more question though.

When the macro writes down the value I was kind of expecting it to write it with the formula.

Is that possible?
 
Upvote 0
Then maybe it should be

Code:
Set e = Selection

and

Code:
ActiveCell.Value = WorksheetFunction.Average(e)
 
Upvote 0
That works, but same result as before.

It is just writing the value rather than the formula in.
 
Upvote 0

Forum statistics

Threads
1,224,541
Messages
6,179,418
Members
452,912
Latest member
alicemil

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