Consulting

Results 1 to 13 of 13

Thread: Solved: Incorporating dynamic ranges into CORREL function using VBA

  1. #1

    Solved: Incorporating dynamic ranges into CORREL function using VBA

    Hi, I'm stuck with a problem on calculating the correlation coefficient of 2 ranges when the ranges are continually expanding on a monthly basis. I have solved this problem previously for functions such as STDEV by selecting the whole column but unfortunately this does not work for the CORREL function.

    Is anyone able to give me an example of how to incorporate a dynamic range into the CORREL function?

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use a dynamic named range, using a formula such as

    =OFFSET($A$1,0,0,COUNTA($A:$A),1)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    BTW, I have Excel 20010, and I can use full columns in CORREL. Do you have an older version that restricts this?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Yes I'm using 2003. I haven't used dynamic ranges before, would you be able to give me an example of how I can incorporate this into the correl function?

    Say for example i need to calculate the correlation between a 2 dynamic ranges in column A and B

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Something like this

    =CORREL(OFFSET($A$1,0,0,COUNTA($A:$A),1),OFFSET($A$1,0,1,COUNTA($A:$A),1))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    BTW, I have Excel 20010, ...

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    Mark,

    I have suggested before that you get on-board, you can't keep these old dinosaur versions indefinitely
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Agreed, but 20,010? VBA537 just seems like such a big jump for my poor head .

    Off to bed for real; a great day to you and yours

  9. #9
    Thanks for this. Based on the above I am applying the formula to columns BA and BB using the formula below:

    ActiveCell.FormulaR1C1 = "=CORREL(OFFSET($BA$1,0,0,COUNTA($BA:$BA),1),OFFSET($BA$1,0,1,COUNTA($BA:$B A),1))"

    However, for some reason I'm getting an error:

    "Run time error 1004. Application defined or object defined error"

    Can you see where my formula is going wrong

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The formula isn't in R1C1 so use

    [VBA]ActiveCell.Formula = "=CORREL(OFFSET($BA$1,0,0,COUNTA($BA:$BA),1),OFFSET($BA$1,0,1,COUNTA($B A:$B A),1))"
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Perfect! Thank you for such a quick resonse!

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Quote Originally Posted by GTO
    Agreed, but 20,010? VBA537 just seems like such a big jump for my poor head .

    Off to bed for real; a great day to you and yours
    Hey Mark its not the first time Bob's indicated he is using a future version.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I know, I am starting to believe it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •