By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,988 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,988 IT Pros & Developers. It's quick & easy.

Excel-Macros-Loops

P: 2
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
Share this Question
Share on Google+
7 Replies


100+
P: 1,646
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 5K+
P: 8,434
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 5K+
P: 8,434
...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

100+
P: 1,646
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 5K+
P: 8,434
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

P: 2
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 5K+
P: 8,434
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.