+ Reply to Thread
Results 1 to 9 of 9

How to show a VBA function's output in the same worksheet cell that calls it?

  1. #1
    Registered User
    Join Date
    08-06-2013
    Location
    the boonies
    MS-Off Ver
    Excel 2007
    Posts
    46

    Thumbs up How to show a VBA function's output in the same worksheet cell that calls it?

    When used in a spreadsheet, I want the result from this VBA function to return the array value "WMKR(MinIndex)" to the same cell that it's called from, without overwriting the function call in the spreadsheet cell. For now I am using ActiveCell.Offset(0,1).Value to put the VBA function result in the adjacent column, but this is not a desireable solution. This workaround currently results in the correct answer in the adjacent cell, and also returns a "#VALUE!" error in the cell where I called "=WName()" (I guess since nothing is returned from the funtion call to that cell).

    If this were a built-in Excel function, the answer to the =function() call would show in the cell, but the formula would remain in the formula box.
    Can you show me how to get my =WName() VBA function to behave the same way? Thanks!

    Please Login or Register  to view this content.
    Last edited by geophysicist; 08-12-2013 at 11:54 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    To return the results of the function, use
    Please Login or Register  to view this content.
    instead of: ActiveCell.Offset(0, 1).Value = WMKR(MinIndex)
    Ben Van Johnson

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    Formulas/Functions that operate within a cell need parameters. Your function can be made to operate that way by moving the key DIMs up into the () so they can be passed in by the formula.

    Here's a very basic example. I've written a UDF called COUNTPRIME that allows a range variable to fed in, this would be a range of cells. The UDF uses a SECOND function (that wasn't required, just wanted the example to be simple to read) that tests the number in each cell of the range to determine if it is a prime number or not.

    Please Login or Register  to view this content.
    (Source for IsPrime)

    Once those two functions are dropped into a standard code module, you will be able to use them in a cell like a regular formula.


    =IsPrime(A1) (only one cell allowed in this function)

    =COUNTPRIME(A1:A20) (this one allows a range of cells to be fed in)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    So your UDF will need to be rewritten to accept parameters the way I've shown, and the result would be fed back into the named function so that it appears in the cell from which it was called.

  5. #5
    Registered User
    Join Date
    08-06-2013
    Location
    the boonies
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    RE: WName = WMKR(MinIndex)

    Thank you for your reply. This recommendation did not work in this case. It did not return any value to the cell (though it did when writing the output in the adjacent cell via the Offset property). JBeaucaire's post below may explain why, though I'm still trying to implement that idea correctly.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    When done, it should look something like:
    Please Login or Register  to view this content.


    If your data presently in columns c, E & F were in adjacent columns, say, D, E & F, then X_Y_MKR would refer to a 2-d array such as D6:F21, and column one of the array would hold XMKR, COL-2 YMKR, COL 3 WMKR

  7. #7
    Registered User
    Join Date
    08-06-2013
    Location
    the boonies
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    sorry - not ready to post

  8. #8
    Registered User
    Join Date
    08-06-2013
    Location
    the boonies
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    Success!!! Thanks to JBeaucaire and protonLeah for your help. Your responses answered my original question, and with your suggestions I modified my code. Slightly different tack on some details as I passed some of the other variables as ranges. I found that, plus getting rid of ActiveCell calls (the way I did them at least) was necessary in order to get the UDF to work in the spreadsheet when copied down the column.

    I have other issues to resolve for this project, but will post them under new threads. This part SOLVED.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-06-2013
    Location
    the boonies
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: How to show a VBA function's output in the same worksheet cell that calls it?

    For completeness, attached is a copy of the spreadsheet.

    Regards.
    Attached Files Attached Files

+ 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] Combined AND function not reading output of the function of another cell
    By Duoae in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-19-2012, 09:22 AM
  2. how to go back to original wrksht that calls another worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2012, 10:24 AM
  3. Why won't my function output to worksheet?
    By PingPing in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-21-2011, 06:25 AM
  4. Format cell to show duration of phone calls
    By Dana in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-10-2006, 11:15 AM
  5. [SOLVED] How do I output the worksheet name in cell?
    By Sloth in forum Excel General
    Replies: 3
    Last Post: 07-08-2005, 07: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