+ Reply to Thread
Results 1 to 5 of 5

how to pass parameter to different sub?

Hybrid View

  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
    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?
    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.
    Dim x As Long
    
    Private Sub CommandButton1_Click()
    x=10
    Call part1
    End Sub
    
    Sub part1()
    MsgBox x
    End Sub
    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:
    Private Sub CommandButton1_Click()
    x=10
    Call part1
    End Sub
    General module:
    Public x As Long
    
    Sub part1()
    MsgBox x
    End Sub
    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.

    Call MyFantasticProcedure(10)
    and the MyF..P... would be

    Sub MyFantasticProcedure(MyVal as Integer)
    'and the code
    Range("A1")=Myval
    
    'would put 10 in A1

    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