Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

I need a excel macro code to carry out a formula.

May 3, 2010 5:24AM PDT

The formula is
D = [ sum from 1 to Nt( {(X - Xave)}^2 / Nt) ]^ 0.5

I need the answer to be in cell F2 and I'm trying to avoid having a new column containing the data
{(X - Xave)}^2 / Nt).

Xs are in Column A

Nt is unknown and this code will be used for different workbooks, same sheet (1) but different values of Nt.

Thanks.

Discussion is locked

- Collapse -
I can see an immediate problem
May 3, 2010 6:49AM PDT

If Nt is unknown, then 1 to Nt is meaningless, and division by Nt is also meaningless.

I suspect you mean that Nt is defined elsewhere, perhaps in each workbook, but could be different for different workbooks.

What is Xave ? I haven't seen that mathematical expression before. Or do you mean an average of all the entries in col A?

Why do you need a macro for this? If there are explanations for the Nt and Xave above, then I'm sure the formula you have given can be worked into any cell, eg F2, even if it has to be worked on a bit.

Any new columns can be hidden of course.

But then your expression starts with D = .... What is D?

Mark

- Collapse -
Clarification
May 3, 2010 5:22PM PDT

Sorry, let me clear up some things.

Nt is the number of Xs i.e the number of rows in column A with data. It is unknown but I know there is a code for finding the last occupied cell in a column so it can be found.

Xave is the average of the data.

D is the value I'm trying to calculate.

E.g Z = x + y where x and y are data in colunms A and B respectively. Z would now be a new colunm populated with x + y.

I need a macro beacause I have more than 2 GB woth of data to work with. I've already done this without a macro for about 20 files and trust me, it's tirirng. I can't copy and paste the formula because the number of cells are different for each case.

- Collapse -
Ahh got it,
May 3, 2010 9:13PM PDT

and thanks for that CNN80.

I can also see now why you need that macro.

Hopefully others here will be able to help with that, but what about working with this in the meantime...?

You may want to work on a couple of dummy, (copied), sheets, saved in a completely different location, in case you make an error and lose anything. In fact, I would copy all of the original workbooks and place them off the hard disk somewhere, a backup.

Do this manually once more. But before doing it, "Record a macro", by going to Macros > Record a macro. Fill in the blanks, Macro name, Shortcut key if you want it, or create an object button, and make it available for any worksheet.

Then carry out the steps you would do manually to make this formula work. When you're done, Stop the recording.

Since all of your data is presented in the same form and same cell locations in each sheet, that same macro should work for each.

Would that work?

Good luck.

Mark

- Collapse -
Nope
May 3, 2010 9:22PM PDT

I've tried that. First of all, the number of the rows are different and the macro record me adding and finding the mean of a certain number of rows. Second, for me to do it manually, I had to create a second column with the values of

((X - Xave)^2)/Nt

then add them, put the value in another cell, then find the square root of that cell.

I couldn't figure out a way to modify the code for any number of cells.