can some1 pls help me? i m beginner in writing some marco.
below r my code
how to pass the x=10 to sub part1?private sub commandbutton1_click() x=10 call part1 end sub sub part1() msgbox x end sub
can some1 pls help me? i m beginner in writing some marco.
below r my code
how to pass the x=10 to sub part1?private sub commandbutton1_click() x=10 call part1 end sub sub part1() msgbox x end sub
Last edited by royUK; 05-13-2008 at 12:58 PM.
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.
If they are in different modules, you will need to declare it as a public variable at the top of a general module.Dim x As Long Private Sub CommandButton1_Click() x=10 Call part1 End Sub Sub part1() MsgBox x End Sub
Worksheet module:
General module:Private Sub CommandButton1_Click() x=10 Call part1 End Sub
HTHPublic x As Long Sub part1() MsgBox x End Sub
Jason
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
Hi,Originally Posted by ccs_1981
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.
and the MyF..P... would beCall MyFantasticProcedure(10)
Sub MyFantasticProcedure(MyVal as Integer) 'and the code Range("A1")=Myval 'would put 10 in A1
HTH
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks