+ Reply to Thread
Results 1 to 7 of 7

Problem with Function that Returns Selected Row in a Specified Worksheet

  1. #1
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    Problem with Function that Returns Selected Row in a Specified Worksheet

    I wish my function to return the row number of the selected cell in a specified inactive worksheet ("Clients"). Instead my function is returning the row number of the active cell of the worksheet I'm working on.

    When I run the function as a macro it returns the desired value.

    Please Login or Register  to view this content.
    The var MyRow is returning the active row number of the sheet I've inserted the function into, not the row number of the selected cell in the "Clients" worksheet. However, the macro version of this program works as I would expect and returns the desired row number:

    Please Login or Register  to view this content.
    Thanks.
    Last edited by Dirigo; 10-02-2011 at 05:50 PM.

  2. #2
    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: Problem with Function that Returns Selected Row in a Specified Worksheet

    Hello Dirigo,

    Welcome to the Forum!

    I tried the macro on my machine and it works. Did you place the macro in a VBA module or in a Worksheet module? It needs to be in a VBA module to work correctly.
    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!)

  3. #3
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    Re: Problem with Function that Returns Selected Row in a Specified Worksheet

    Quote Originally Posted by Leith Ross View Post
    Hello Dirigo,

    Welcome to the Forum!

    I tried the macro on my machine and it works. Did you place the macro in a VBA module or in a Worksheet module? It needs to be in a VBA module to work correctly.
    Thanks.

    The function is placed in the Modules Folder of my workbook with all the other macros so I think it's in the right place.
    Last edited by Dirigo; 10-02-2011 at 06:27 PM.

  4. #4
    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: Problem with Function that Returns Selected Row in a Specified Worksheet

    Hello Dirigo,

    You have it in the right place. Is this the only macro in the workbook that isn't working correctly?

  5. #5
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    Re: Problem with Function that Returns Selected Row in a Specified Worksheet

    Quote Originally Posted by Leith Ross View Post
    Hello Dirigo,

    You have it in the right place. Is this the only macro in the workbook that isn't working correctly?
    Yes, everything else seems to be working correctly. When I run the function from the VBA editor, it seems to work fine. But when I place it in a cell as =MyFunction() and update the value, the var MyRow incorrectly reflects the row of the cell in the active worksheet, not the "Clients" worksheet.

    This is my first User Defined Function. Is there something I'm missing?

    BTW, earlier I thought the function was properly working in another workbook, but now I cannot even replicate that.
    Last edited by Dirigo; 10-02-2011 at 06:54 PM.

  6. #6
    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: Problem with Function that Returns Selected Row in a Specified Worksheet

    Helo Dirigo,

    I just remembered when using UDFs there are limitations on what you can do. Basically, any VBA function that moves the worksheet cursor internally will produce an error. You can not activate another sheet in a UDF. You retrieve a cell's value using the Worksheet Index function but a sheet needs to be activated before you can retrieve the ActiveCell. The example below will return the value of cell A5 to the cell with the UDF. Confusing, I know.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    Re: Problem with Function that Returns Selected Row in a Specified Worksheet

    Quote Originally Posted by Leith Ross View Post
    Helo Dirigo,

    I just remembered when using UDFs there are limitations on what you can do. Basically, any VBA function that moves the worksheet cursor internally will produce an error. You can not activate another sheet in a UDF. You retrieve a cell's value using the Worksheet Index function but a sheet needs to be activated before you can retrieve the ActiveCell. The example below will return the value of cell A5 to the cell with the UDF. Confusing, I know.
    Darn. At least I won't waste any more time on that method. I guess I will pass the row number to the function as a public variable defined in a macro.

    Thanks.
    Last edited by Dirigo; 10-02-2011 at 08:36 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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