Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Placement of variables in called sub causes code to fail
Can somebody help me understand why the following code, in sub1 fails
to run but the modifcations to the called sub below that does run? I was getting "Argument not optional" error. Alan Sub1() 'in module1 .....other routines If bla bla then Exporttofile 'in module2 Else .... other routines Sub Exporttofile (fName As String, Sep As String, SelectionOnly As Boolean) code... Modified Exporttofile: Sub Exporttofile() Dim fName As String Dim Sep As String Dim SelectionOnly As Boolean |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Placement of variables in called sub causes code to fail
One thing I noticed is that you do not specify how the variable is to be
passed, i.e. ByVal, ByRef, Optional...That could have triggered the error message. "acampbell" wrote: Can somebody help me understand why the following code, in sub1 fails to run but the modifcations to the called sub below that does run? I was getting "Argument not optional" error. Alan Sub1() 'in module1 .....other routines If bla bla then Exporttofile 'in module2 Else .... other routines Sub Exporttofile (fName As String, Sep As String, SelectionOnly As Boolean) code... Modified Exporttofile: Sub Exporttofile() Dim fName As String Dim Sep As String Dim SelectionOnly As Boolean |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Placement of variables in called sub causes code to fail
On Mar 23, 8:39 am, JLGWhiz wrote:
One thing I noticed is that you do not specify how the variable is to be passed, i.e. ByVal, ByRef, Optional...That could have triggered the error message. "acampbell" wrote: Can somebody help me understand why the following code, in sub1 fails to run but the modifcations to the called sub below that does run? I was getting "Argument not optional" error. Alan Sub1() 'in module1 .....other routines If bla bla then Exporttofile 'in module2 Else .... other routines Sub Exporttofile (fName As String, Sep As String, SelectionOnly As Boolean) code... Modified Exporttofile: Sub Exporttofile() Dim fName As String Dim Sep As String Dim SelectionOnly As Boolean- Hide quoted text - - Show quoted text - To add onto JLGWhiz' post, in your coded line of "Sub Exporttofile()" you do not specify that any arguments are being "passed" to the sub procedure. As JLGWhiz said, when you pass arguments to another sub procedure, you have to define whether they are ByRef, ByVal, or Optional. If you do not specify, then the default is ByRef. For example, in your code you would need to state something like this in order for the variables to be passed "Sub Exporttofile(ByVal fName, ByVal Sep, ByVal SelectionOnly)" This will allow you to use the values of the "fName, Sep, and SelectionOnly" variables from your "Sub1" to be used in your sub "Exporttofile." Note, if you pass these variables from Sub1 to Exporttofile, you don't need the "Dim fName, Dim Sep, and Dim SelectionOnly" declarations in Exporttofile. Matt |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Placement of variables in called sub causes code to fail
the original declaration for exporttofile requires that 3 arguments be passed
to the function and you do not pass any arguments when you call it. This would be appropriate if the calling procedure needs to pass this information to the sub or receive this information from the sub or both. you would then call it like Sub1() 'in module1 Dim s1 as String, s2 as String, b as Boolean .....other routines If bla bla then Exporttofile s1, s2, b 'in module2 Else thus if you call it with the required arguments correctly dimensioned, then it works. In the second declaration for exporttofile, exporttofile requires no information and returns no information. All variables are defined and populated from information/code within the subroutine. Thus when you call it with no arguments and it requires no arguments, it works. -- Regards, Tom Ogilvy "acampbell" wrote: Can somebody help me understand why the following code, in sub1 fails to run but the modifcations to the called sub below that does run? I was getting "Argument not optional" error. Alan Sub1() 'in module1 .....other routines If bla bla then Exporttofile 'in module2 Else .... other routines Sub Exporttofile (fName As String, Sep As String, SelectionOnly As Boolean) code... Modified Exporttofile: Sub Exporttofile() Dim fName As String Dim Sep As String Dim SelectionOnly As Boolean |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Placement of variables in called sub causes code to fail
On Mar 23, 12:23 pm, Tom Ogilvy
wrote: the original declaration for exporttofile requires that 3 arguments be passed to the function and you do not pass any arguments when you call it. This would be appropriate if the calling procedure needs to pass this information to the sub or receive this information from the sub or both. you would then call it like Sub1() 'in module1 Dim s1 as String, s2 as String, b as Boolean .....other routines If bla bla then Exporttofile s1, s2, b 'in module2 Else thus if you call it with the required arguments correctly dimensioned, then it works. In the second declaration for exporttofile, exporttofile requires no information and returns no information. All variables are defined and populated from information/code within the subroutine. Thus when you call it with no arguments and it requires no arguments, it works. -- Regards, Tom Ogilvy "acampbell" wrote: Can somebody help me understand why the following code, in sub1 fails to run but the modifcations to the called sub below that does run? I was getting "Argument not optional" error. Alan Sub1() 'in module1 .....other routines If bla bla then Exporttofile 'in module2 Else .... other routines Sub Exporttofile (fName As String, Sep As String, SelectionOnly As Boolean) code... Modified Exporttofile: Sub Exporttofile() Dim fName As String Dim Sep As String Dim SelectionOnly As Boolean- Hide quoted text - - Show quoted text - Thank you both for the clarification for properly coding for calling the second routine. Alan |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
order placement of code | Excel Programming | |||
Placement of code | Excel Programming | |||
Carrying Variables into Called Macros | Excel Programming | |||
Placement of Code | Excel Programming | |||
Input variables on Sheet1 being called by a VB program on Sheet 2 | Excel Programming |