Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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   Report Post  
Posted to microsoft.public.excel.programming
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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
order placement of code Curt Excel Programming 0 March 19th 07 05:34 AM
Placement of code Hank Youngerman Excel Programming 3 November 17th 06 05:06 PM
Carrying Variables into Called Macros Marty Excel Programming 2 June 12th 04 01:23 AM
Placement of Code Phil Hageman[_3_] Excel Programming 7 January 30th 04 02:06 PM
Input variables on Sheet1 being called by a VB program on Sheet 2 RU42 Excel Programming 1 August 23rd 03 09:50 AM


All times are GMT +1. The time now is 11:28 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"