467,161 Members | 912 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

Excel-Macros-Loops

Here is the issue.

In a workbook I have 100 sheet with data at column b2:b1700 in everyone of them. I want to calculate the correlation btw all possible pairs. For sheet2 and sheet3 the function is =CORREL(sheet2!R2C2:R1700C2,sheet3!R2C2:R1700C2)

I have tried to create a macro

For i = 2 To Sheets.Count
x = Sheets(i).Name
Sheets(1).Cells(1, i).FormulaR1C1 = "=CORREL(x!R2C2:R1700C2,sheet3!R2C2:R1700C2)"
Next i

When I run the macro it wants me to specify a file x to read from instead of going to the currect sheet.
Eliminating the second line and replacind x with i in the correlation same thing happens.
I have tried a lot of things...

Does anybody know how to do it?
Nov 8 '06 #1
  • viewed: 2894
Share:
7 Replies
1GB
Here is the issue.

In a workbook I have 100 sheet with data at column b2:b1700 in everyone of them. I want to calculate the correlation btw all possible pairs. For sheet2 and sheet3 the function is =CORREL(sheet2!R2C2:R1700C2,sheet3!R2C2:R1700C2)

I have tried to create a macro

For i = 2 To Sheets.Count
x = Sheets(i).Name
Sheets(1).Cells(1, i).FormulaR1C1 = "=CORREL(x!R2C2:R1700C2,sheet3!R2C2:R1700C2)"
Next i

When I run the macro it wants me to specify a file x to read from instead of going to the currect sheet.
Eliminating the second line and replacind x with i in the correlation same thing happens.
I have tried a lot of things...

Does anybody know how to do it?
Hi. Interesting problem. I see that you have included the variable x in the formula string. Excel will not know the value of x when it looks at the formula.
This might work better:
Expand|Select|Wrap|Line Numbers
  1. stFormula = "=CORREL(" _
  2.              & Sheets(i).Name _
  3.              & "!R2C2:R1700C2,sheet3!R2C2:R1700C2)"
  4.  
  5. Sheets(1).Cells(1, i).FormulaR1C1 = stFormula
  6.  
Good luck
Nov 8 '06 #2
Expert 8TB
Hi. Interesting problem. I see that you have included the variable x in the formula string. Excel will not know the value of x when it looks at the formula.
...
Actually, I think the only problem with the original code is that the x was inside the quotes, and thus inserted directly as part of the string, rather than its value being used. Anyway...

Do you think this would work?
Expand|Select|Wrap|Line Numbers
  1. For I = 1 To Sheets.Count - 1
  2.   For J = I + 1 To Sheets.Count
  3.     Sheets(1).Cells(1, I).FormulaR1C1 = "=CORREL(" & _
  4.       Sheets(I).Name & "!R2C2:R1700C2, " & _
  5.       Sheets(J).Name & "!R2C2:R1700C2)"
  6.   Next I
  7. Next
The idea of this is to use a nested FOR loop to go through all the possible combinations - 9,801 of them, I think. Or was the intention only to check each sheet against the next one, or what?
Nov 9 '06 #3
Expert 8TB
...Or was the intention only to check each sheet against the next one, or what?
Yeah, on second thought my code would be simply overwriting the same values 99 times. So I guess the intention must have been just to check each sheet against the next. Or possibly each of the 50 pairs of sheets.
Nov 9 '06 #4
1GB
Yeah, on second thought my code would be simply overwriting the same values 99 times. So I guess the intention must have been just to check each sheet against the next. Or possibly each of the 50 pairs of sheets.
Hey Killer.
Are you getting a little punch drunk with these questions? ;)

Now you are answering questions that have already been answered. Get some sleep :)
Nov 9 '06 #5
Expert 8TB
Hey Killer.
Are you getting a little punch drunk with these questions? ;)

Now you are answering questions that have already been answered. Get some sleep :)
Not only that, but I'm criticising my own posts. :)

Actually, while I could do with more sleep, it's lunch time here. But I'm doing about 28 things at once, plus jumping in and out of these forums, etc.

I'm going to have to cut back on something, and theScripts doesn't pay the bills.
Nov 9 '06 #6
Thank you very very much

With some loops I made it work nicely but it takes a little time to calculate all the pairs.

Thanks again
Nov 9 '06 #7
Expert 8TB
Thank you very very much
With some loops I made it work nicely but it takes a little time to calculate all the pairs.
I suppose you can only expect so much. I'm not familiar with the CORREL function, but to process 99 (or 50, or whatever) lots of 1700 cells has to take some time.

By the way, just out of curiosity, could you show us the code you ended up with?
Nov 9 '06 #8

Post your reply

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

Similar topics

13 posts views Thread by Allison Bailey | last post: by
6 posts views Thread by Matthew Wieder | last post: by
14 posts views Thread by pmud | last post: by
22 posts views Thread by Howard Kaikow | last post: by
7 posts views Thread by Alain \Mbuna\ | last post: by
16 posts views Thread by alexia.bee@gmail.com | last post: by
9 posts views Thread by Doug Glancy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.