+ Reply to Thread
Results 1 to 5 of 5

Transforming array multiplication from Excel to VBA function

  1. #1
    Registered User
    Join Date
    07-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Transforming array multiplication from Excel to VBA function

    I'm new to VBA and just starting to try working with array formulas. I have two functions that I can't figure out how to do in VBA.

    1. I want to create a function called =ifIs(CheckItem,CheckRange)
    CheckItem is a cell that has string. CheckRange is a range with a string array.

    if checkItem is blank, I'd like ifIs to return an array of 1s the length of the CheckRange.
    If checkItem is a string, I'd like ifIs to return and of 1 if that matches that column in checkRange, and 0 if it does not match.

    The array formula in Excel is
    ={IF(CheckItem="",Ones,--(CheckRange=CheckItem))}

    "Ones" is a range name that refers to a column of ones the same length as CheckRange. I'd like the udf to have only two inputs, however, checkItem and checkRange.

    Ideally, I'd also like ifIs() to not have any loops. The function will be used on large columns of numbers so speed of execution is important.

    2. I'd also like to create a UDF called TMM(vector,Matrix)

    The TMM function would return as an array with the same number of columns as the matrix.

    In the example spreadsheet,
    =MMULT(TRANSPOSE(B10:B14),C3:E7)
    would return {25 24 25}

    My goal is to use these functions as building blocks for bigger formulas. I'd be grateful for any help getting started.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: Transforming array multiplication from Excel to VBA function

    Hi

    Sorry, I'm just leaving, don't have time to look into the file.

    For your first problem, however, if the problem is the Ones range, that you don't want, I think you don't need it.

    I think your array formula can be written as:

    =0+((CheckItem="")+(CheckRange=CheckItem)>0)

    This formula only uses the 2 inputs, as you want .

    Does this help?

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Transforming array multiplication from Excel to VBA function

    Hello gren,

    There are issues with coding UDFs (User Defined Functions). Click this link Excel User-Defined Functions to learn more.

    Not really sure why you want to complicate matters by using VBA to create worksheet functions when the native functions are already available. Native worksheet functions incur less processing time when called directly than calling them from VBA, performing the function, and transferring it back to the Excel worksheet.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    07-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Transforming array multiplication from Excel to VBA function

    Quote Originally Posted by lecxe View Post
    I think your array formula can be written as:

    =0+((CheckItem="")+(CheckRange=CheckItem)>0)

    Does this help?
    This is very clever... I was thinking a UDF was the best way to get away from nesting ifs, but this works great. What does the zero at the beginning do?

  5. #5
    Registered User
    Join Date
    07-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Transforming array multiplication from Excel to VBA function

    I have two objectives in looking for a VBA solution:
    1) to make the formulas easier to read. I start with formulas written in matrix notation, e.g. (Region=North.Rep=Joe)'(Sales.Product=....), and by the time the formula is written in Excel it takes 5 lines of text and is very hard to decipher. I have one solution, which is to hand the data to R for matrix work and get it back for presentation, but that means I can't share the file.
    2) I haven't figured out how to do any work with arrays in VBA except by looping through them. My hope is to learn how to do these basic matrix transformations in VBA because I've run into several things that are fast in a formula but slow in VBA.

    If I could have calculation times a little slower than Excel (2-3x calculation time) but with more clarity, that would be a good trade-off.

+ 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