Placement of variables in called sub causes code to fail

A

acampbell

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
 
G

Guest

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.
 
M

matt

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.








- 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
 
G

Guest

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.
 
A

acampbell

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








- Show quoted text -

Thank you both for the clarification for properly coding for calling
the second routine.

Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top