+ Reply to Thread
Results 1 to 21 of 21

Using cell ranges & arrays in VBA functions?

  1. #1
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Using cell ranges & arrays in VBA functions?

    Hi,

    I'm new to the forum and have searched all over for help with this problem but can't find a satisfactory answer.

    I want to create a function that can take a range of cells as an argument, manipulate the data and then return an answer. So for instance, as a practise example I've been trying to create my own 'sum' function mySum that would take the following form in an example:

    • Cells A1, A2, A3 would have numerical values
    • Another cell, e.g. A4 would take the form =mySum(A1:A3)
    • mySum formula sum the values in A1:A3

    I'd like to do this by putting the contents of cells A1:A3 in an array and then summing the contents of that array, so I understand the mechanics of using arrays in this way within VBA.

    Can anyone help!?

    Thanks!

    JD

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Using cell ranges & arrays in VBA functions?

    This might be a good place to start: http://msdn.microsoft.com/en-us/libr.../gg264233.aspx
    This might also be a good place to start: http://office.microsoft.com/en-us/ex...996.aspx?CTT=1

    It might look something like:
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Thanks, that makes sense but what I'm looking to do is put the range into a array and then manipulate the array. My example was just to demonstrate what I mean. Eg I want to use the 3rd value in the array that is constructed from a selected range of sells.

    So =myFunction(A1:A8) would take range A1:A8 and put it in an array and then return the value of the 3rd element of that array.

    JD

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using cell ranges & arrays in VBA functions?

    Please Login or Register  to view this content.
    http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
    Last edited by patel45; 09-13-2013 at 02:17 PM.
    If solved remember to mark Thread as solved

  5. #5
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Thanks patel45, but am looking to do this using a function that takes the cell range as an argument. So e.g in the cell that uses the function (and returns the calculated answer) it would take the form =myFunction(A1:A3)

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using cell ranges & arrays in VBA functions?

    How about this?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Nope pls see my follow up comments

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using cell ranges & arrays in VBA functions?

    This uses an array.
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using cell ranges & arrays in VBA functions?

    Quote Originally Posted by jdelin86 View Post
    Thanks patel45, but am looking to do this using a function that takes the cell range as an argument. So e.g in the cell that uses the function (and returns the calculated answer) it would take the form =myFunction(A1:A3)
    Now you have the solution with array, then you can transform the sub in a function

  10. #10
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Thanks Norie, your example makes sense. However, when I replicate it / modify it it stops working and gives me a VALUE error.

    For instance, as a test I modified your original code to:

    Please Login or Register  to view this content.
    which returns the first element in the array as expected.

    However, when I use the exact same code / logic for a differently named function (with differently named variables) I get VALUE errors and it appears the array I'm attempting to create is not actually an array.

    Can you / anyone pls assist? This is driving me mad as I can't see where Im going wrong!

    Thanks

    JD

  11. #11
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using cell ranges & arrays in VBA functions?

    your code works, if you change it , paste the new one or better attach a sample file

  12. #12
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Weird.

    Here's an example:

    Please Login or Register  to view this content.
    So the first function works, but the second function gives a VALUE error.

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Using cell ranges & arrays in VBA functions?

    what range are you passing to the function? if it's one cell, you won't have an array
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  14. #14
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Exact same range in both instances: =mySum(B4:B6) and =test(B4:B6)

  15. #15
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using cell ranges & arrays in VBA functions?

    on first code you define arr then you don't use it, but sumrange
    Please Login or Register  to view this content.
    on the second you use myarray and the code is wrong, the correct one is
    Please Login or Register  to view this content.
    have a look at my first post
    Last edited by patel45; 09-16-2013 at 06:14 AM.

  16. #16
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Legend! Thank you so much, knew it had to be something silly like that.

    Appreciate your help and patience

  17. #17
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    sorry, got another issue...

    I've started building the function I originally intended to use arrays with, but am getting VALUE errors again.

    I wish to define an array 'retArr' which will hold the values from the range I pass to the function. I then wish to square each value in the array and put those squared values in an array called 'sqRet'.

    However, when I test the function at the end to check it is squaring the values, I get a VALUE error...

    Please Login or Register  to view this content.

  18. #18
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Using cell ranges & arrays in VBA functions?

    you have not declared sqRet as an array
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Adding the parentheses doesnt seem to make any difference (still get the VALUE error)...

  20. #20
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Using cell ranges & arrays in VBA functions?

    did you add the ReDim line as well?

  21. #21
    Registered User
    Join Date
    09-13-2013
    Location
    London, England
    MS-Off Ver
    Excel 2011
    Posts
    12

    Re: Using cell ranges & arrays in VBA functions?

    Ah, that'll do it. Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Confused about arrays and ranges in functions
    By Llurker in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  2. [SOLVED] Confused about arrays and ranges in functions
    By Llurker in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 07:05 AM
  3. [SOLVED] Confused about arrays and ranges in functions
    By Llurker in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 06:05 AM
  4. [SOLVED] Confused about arrays and ranges in functions
    By Llurker in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  5. [SOLVED] Confused about arrays and ranges in functions
    By Llurker in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1