+ Reply to Thread
Results 1 to 4 of 4

Passing arguments to a macro in another file

  1. #1
    Registered User
    Join Date
    06-05-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Passing arguments to a macro in another file

    Hi,

    I use the following macro "modifystri" in a workbook "Modify" to pass a variable to a macro ("macro1") located in a sheet ("Sheet1") in another workbook ("Test.xls")

    Please Login or Register  to view this content.

    The definition of macro1 located in workbook "Test.xls" is:

    Please Login or Register  to view this content.
    The value of stri after calling "macro1" is still empty. I want to use "macro1" to modify the contents of stri. Please help.
    Last edited by abhimanyut; 06-05-2009 at 05:43 AM. Reason: Put code tags around code snippet

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,440

    Re: Passing arguments to a macro in another file

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here


    Couple of things. The value of stri is passed rather than the variable so you have nothing via which to return a value.

    Instead of a subroutine use a function to return a value.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    The other problem is that the function does not return a value if it is within a sheet module. So either move the function to a standard code module or write a new function in a code module within the workbook that uses the sheet level code.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-05-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Passing arguments to a macro in another file

    Thanks for the suggestion. But aren't arguments by default passed by reference in VBA? In which case, the macro should modify the value.

    Also, I wish to pass more than one strings to the macro and modify them. I know I can concatenate their modified values inside the function and return the single result string, but is there any way I can pass these variables to a macro residing in a different workbook?

    Thanks for the point regarding not putting functions in a sheet module!

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,440

    Re: Passing arguments to a macro in another file

    Normally yes they are. I'm not sure this is true when you use the .Run method

    You could return an array
    Last edited by Andy Pope; 06-05-2009 at 06:19 AM.

+ 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