+ Reply to Thread
Results 1 to 4 of 4

Using array constants in user-defined excel vba function

  1. #1
    Registered User
    Join Date
    10-01-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Using array constants in user-defined excel vba function

    I have a function in VBA of the type

    Function MyFunc(Indx As Integer, k As Long, Rho As Range, A As Range) As Variant .... End Function

    which is called as a user-defined function from within the Excel worksheet. When called with the last two arguments being a range (i.e. Result = MyFunc(1,98,A1:A2, B1:B2)) it works fine. However, when I try to directly use an array constant instead of a range (i.e. Result = MyFunc(1,98,{10,11}, {20,30}), it returns a #VALUE error.

    I thought I could fix it by redefining the last two arguments as arrays of type double, but this didn't work either (i.e. Function MyFunc(Indx As Integer, k As Long, Rho() As Double, A() As Double) As Variant .... End Function ).

    Does someone have a suggestion for a flexible solution, which would permit either calling method: by range, as well as by an array constant?

    Thanks in advance!
    Last edited by MiloMan; 10-01-2009 at 08:58 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Using array constants in user-defined excel vba function

    Try passing as a Variant.

    Some of the function code may need to change as well.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-01-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Using array constants in user-defined excel vba function

    Quote Originally Posted by shg View Post
    Try passing as a Variant.

    Some of the function code may need to change as well.
    Thank you! This solved it - I can now call the function with both types of input arguments, and if I need to find out what it's been called with, I can just use TypeName.

    Thanks again,
    MiloMan

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Using array constants in user-defined excel vba function

    Great. Would you please mark the thread as Solved?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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