+ Reply to Thread
Results 1 to 5 of 5

how to pass parameter to different sub?

  1. #1
    Registered User
    Join Date
    04-23-2008
    Posts
    48

    Smile how to pass parameter to different sub?

    can some1 pls help me? i m beginner in writing some marco.
    below r my code
    Please Login or Register  to view this content.
    how to pass the x=10 to sub part1?
    Last edited by royUK; 05-13-2008 at 12:58 PM.

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Are both of those procedures in the same Worksheet module, or is the second in a general module?

    If they are both in the same module, you would need to declare the variable at the top of the module, outside the other procedures.
    Please Login or Register  to view this content.
    If they are in different modules, you will need to declare it as a public variable at the top of a general module.

    Worksheet module:
    Please Login or Register  to view this content.
    General module:
    Please Login or Register  to view this content.
    HTH

    Jason

  3. #3
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Birmingham, England
    MS-Off Ver
    12.0
    Posts
    808
    I reckon you should declare the x = 10 outside the sub at the very top that ways its public and can be used by any sub routine
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Quote Originally Posted by ccs_1981
    can some1 pls help me? i m beginner in writing some marco.
    below r my code

    private sub commandbutton1_click()
    x=10
    call part1
    end sub

    sub part1()
    msgbox x
    end sub

    how to pass the x=10 to sub part1?
    Hi,

    I note your thread title was asking how to pass a parameter to another sub. The replies you've had, whilst achieving what you want, rely on explicitly declaring a variable, using that and ensuring that it is a) still in scope, and b) its 'life' hasn't passed.

    Passing a parameter is somewhat different. The pair of brackets that you see at the end of every Procedure name, e.g. MyFantasticProcedure() are where the receiving Procedure defines the value that's being passed. So for instance if you want to pass the number 10, you'd use the following code.

    Please Login or Register  to view this content.
    and the MyF..P... would be

    Please Login or Register  to view this content.

    HTH

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    ccs_1981

    Please read the Forum Rules & wrap your code with Code tags in future.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ 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