Average in a For Next Loop Macro

dludwig

New Member
Joined
Mar 14, 2011
Messages
3
I am trying to calculate averages in multiple columns using a macro and a for next loop. The code runs, but after running it gives me the following error: "Run-time error '1004': Unable to get the average property of the Worksheet function class". I used search on this message board, but I couldn't find how to fix my problem.

And "yes" a worksheet function will do this, but "no" I'm not going to use a worksheet function. There are too many calculations and this is very repetitive, I need to use numbers pasted into cells.

The portion of code giving me problems is:
.
.
.
End If
Cells(22, i + 1).Value = WorksheetFunction.Average(Range(Cells(31, i + 1), Cells(40, i + 1)))
Cells(20, i + 1).Value = (Cells(30, i + 1) / Cells(22, i + 1)) ^ 2
Next i
.
.

I've tried "With"/"End With" to specify the sheet, but what I tried didn't work:
.
.
.
End If
With Worksheets("Data")
Cells(22, i + 1).Value = WorksheetFunction.average (.Range(.Range(Cells(31, i + 1)), .Range(Cells(40, i + 1))))
End With
Cells(20, i + 1).Value = (Cells(30, i + 1) / Cells(22, i + 1)) ^ 2
Next i
.
.
I usually just use the macro recorder with a few small changes on my part, but this has me stumped and any help woud be greatly appreciated. And if all else fails, for j = 1 to 10 divided by 10, but that kinda reminds of the Geico caveman commercials.

Thank u,
dludwig
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try like this

Code:
With Worksheets("Data")
    .Cells(22, i + 1).Value = WorksheetFunction.Average(.Range(.Cells(31, i + 1), .Cells(40, i + 1)))
End With
 
Upvote 0
Thank you VoG for taking a look at it. I copied/pasted your lines of code into my module, and rem'ed out my code.

It still results in the same error with your code, "Unable to get the average property of the Worksheet function class".

I forgot to mention in my first post, I'm using Excel 2007 for windows.

Any more help will be greatly appreciated.

dludwig
 
Upvote 0
VoG, thank you for the help again.

I made a silly error copying/pasting. Fixed the error, IT WORKS!

Thanks again,
dludwig
 
Upvote 0

Forum statistics

Threads
1,218,620
Messages
6,143,523
Members
450,492
Latest member
Rusbus1972

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