473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Writing a macro to calculate average of cells

1 New Member
Hi Everybody,
I need to write a macro on my Excel sheet to calculate the average of cells for me. This is the code I have written but the average function is not working.
Can you please let me know how can I use the Average function in my code.
Thanks alot

Expand|Select|Wrap|Line Numbers
  1. Sub Macro1()
  2. Ncol = 1
  3. nrowColumn2 = 1
  4. i = 1
  5. Do Until IsEmpty(Worksheets(1).Cells(i, 1))
  6.   j = i + 7
  7.   Worksheets(1).Cells(i, 2).Select
  8.  
  9.   ActiveCell.Value2 = "=
  10.   AVERAGE(Worksheets(1).Cells(i,1).value2:Worksheets(1).Cells(j,1).value2)"
  11.  
  12.   Modi = i Mod 3
  13.   If Modi = 0 Then
  14.      i = i + 10
  15.   Else
  16.      i = i + 1
  17.   End If
  18.   nrowColumn2 = nrowColumn2 + 1
  19. Loop
  20. End Sub
Aug 10 '07 #1
1 7274
Killer42
8,435 Recognized Expert Expert
You're including the variable names i & j in the formula. Is that what you intended? I wouldn't think that Excel would accept them when calculating.

In other words, if you simply typed this formula into a cell in an Excel spreadsheet, would you expect it to work? (I'm not saying it won't, as I don't know for sure)

=AVERAGE(Worksheets(1).Cells(i,1).value2:Worksheet s(1).Cells(j,1).value2)

Ok, I just tried pasting it in, and Excel obviously doesn't like it. I would suggest you're getting your variable names and their values confused. You probably need to build that string quite differently. Could you explain in more detail what is supposed to go into the formula? For instance, does "Worksheets(1).Cells(i,1).value2" refer to the start of the range you want to average? Or does it refer to a cell whose value identifies the start of the range? (I mean, perhaps this cell contains "$A$7" or something, and you want to use that as a cell reference). And why value2 rather than value?
Aug 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: derelict | last post by:
Hey all, im getting desperate now. I have a macro running in Word 2003, when I run the macro it *should* put a 'bottom' cell border in each cell that has the style used - this included a border at...
0
by: lavicool | last post by:
Hi all, I just joined the post. Iam new to C programming. I have a question regarding a problem for my assignment. The problem is i have a input file with student ID, 5 program scores, midterm and...
4
by: gaga | last post by:
hi guys, a part of my program requires me to calculate an average of items that are sold. the easiest way to do that would be writing a function, but im having trouble making up the parameters. if...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
3
by: asciiletters | last post by:
I have a spreadsheet with cells that use randbetween(). At the bottom of each column there is a sum() cell for these cells. I made a macro that refreshes the values until a column's total is within...
3
by: paeh | last post by:
hello..can anyone help me. I am beginner in programming. I need to make a system that can calculate moving average. my system process will be executed according to certain schedule such as daily,...
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
4
by: John Brock | last post by:
I have a .NET application that, among other things, creates Excel workbooks, and I have run into a very strange problem involving formulas on one worksheet that reference values on another...
4
by: pbj2009 | last post by:
Hello all: I'm pretty stumped on this one. I'm not looking for Code, I'm just trying to figure out the best way to start this since I am new to reading and writing from files. I can't figure out...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.