View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
matt matt is offline
external usenet poster
 
Posts: 73
Default 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