PC Review


Reply
Thread Tools Rate Thread

how to call a sub that has arguments

 
 
Harold Good
Guest
Posts: n/a
 
      23rd Dec 2009
Hi, I'm trying to call this procedure located in the MyCode module:
Sub Worksheet_Change(ByVal Target As Range)
code here
End Sub
(the above code works properly when it is in the Sheet1 (Budget) module)

From this procedure which is in the Sheet1 (Budget) module. How do I
write the code below? The best I can come up with is:
Sub YearChange()
Call MyCode.Worksheet_Change
End Sub

It does not work, I don't know how to handle the argument within the
parenthesis.

Also, I don't think I need to include the MyCode as part of the path,
but a similar Call procedure I've done without arguments only works if I
include this in the path. What might I have done wrong.

I would appreciate anyone's help with writing the bottom code correctly.

Gratefully,
Harold
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      23rd Dec 2009
The title line of the code:

Sub Worksheet_Change(ByVal Target As Range)

identifies it as worksheet event code. In this case, it is code that
executes when there is a change on the worksheet. Correctly written it
would be:

Private Sub Worksheet_Change(ByVal Target As Range).

That said, if you want the code to execute without making a change to the
worksheet, then the easy way is to change the title line to some other name
like:

Sub myChangedSub()
'Code here
End Sub

Then you can put it in the public code module and call it like:

Sub YearChange()
Call myChangedSub
End Sub

Or just run it instead of calling it. But you might have to change some of
the code to make it work in the public module for the specific sheet that
you want the data to apply to.




"Harold Good" <(E-Mail Removed)> wrote in message
news:%23$(E-Mail Removed)...
> Hi, I'm trying to call this procedure located in the MyCode module:
> Sub Worksheet_Change(ByVal Target As Range)
> code here
> End Sub
> (the above code works properly when it is in the Sheet1 (Budget) module)
>
> From this procedure which is in the Sheet1 (Budget) module. How do I write
> the code below? The best I can come up with is:
> Sub YearChange()
> Call MyCode.Worksheet_Change
> End Sub
>
> It does not work, I don't know how to handle the argument within the
> parenthesis.
>
> Also, I don't think I need to include the MyCode as part of the path, but
> a similar Call procedure I've done without arguments only works if I
> include this in the path. What might I have done wrong.
>
> I would appreciate anyone's help with writing the bottom code correctly.
>
> Gratefully,
> Harold



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      23rd Dec 2009
MyCode is the codename of the worksheet, right?

Remove the word Private from that worksheet_change procedure line:

I had this in the myCode worksheet module:

Option Explicit
Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
End Sub

Then I called that procedure using:

Option Explicit
Sub testme()
Call myCode.Worksheet_Change(Target:=myCode.Range("a1"))
End Sub


This makes it look like A1 on that sheet changing--and making the
Worksheet_change fire.



Harold Good wrote:
>
> Hi, I'm trying to call this procedure located in the MyCode module:
> Sub Worksheet_Change(ByVal Target As Range)
> code here
> End Sub
> (the above code works properly when it is in the Sheet1 (Budget) module)
>
> From this procedure which is in the Sheet1 (Budget) module. How do I
> write the code below? The best I can come up with is:
> Sub YearChange()
> Call MyCode.Worksheet_Change
> End Sub
>
> It does not work, I don't know how to handle the argument within the
> parenthesis.
>
> Also, I don't think I need to include the MyCode as part of the path,
> but a similar Call procedure I've done without arguments only works if I
> include this in the path. What might I have done wrong.
>
> I would appreciate anyone's help with writing the bottom code correctly.
>
> Gratefully,
> Harold


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      23rd Dec 2009
You could also use this:

Option Explicit
Sub testme()
Application.Run "'" & ThisWorkbook.Name _
& "'!myCode.Worksheet_Change", myCode.Range("a1")
End Sub

And keep the private in the procedure statement:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
End Sub


=========
You can determine the code name of the sheet by:
Go into the VBE
Show the project explorer (hit ctrl-r)
Select your workbook's project
Select the worksheet that owns the code
Hit F4 to see the properties.

The Codename is the (name) property in that list of properties. The Name
property (without the ()'s) is the name that the user sees on the sheet tab in
excel.



Harold Good wrote:
>
> Hi, I'm trying to call this procedure located in the MyCode module:
> Sub Worksheet_Change(ByVal Target As Range)
> code here
> End Sub
> (the above code works properly when it is in the Sheet1 (Budget) module)
>
> From this procedure which is in the Sheet1 (Budget) module. How do I
> write the code below? The best I can come up with is:
> Sub YearChange()
> Call MyCode.Worksheet_Change
> End Sub
>
> It does not work, I don't know how to handle the argument within the
> parenthesis.
>
> Also, I don't think I need to include the MyCode as part of the path,
> but a similar Call procedure I've done without arguments only works if I
> include this in the path. What might I have done wrong.
>
> I would appreciate anyone's help with writing the bottom code correctly.
>
> Gratefully,
> Harold


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re : Excel To Call Sub and Assign Arguments tkt_tang@hotmail.com Microsoft Excel Programming 6 11th Sep 2008 01:36 AM
Invalid Procedure Call or Arguments yanto Microsoft Access Form Coding 2 4th Dec 2007 03:38 AM
Call sub with arguments from .onaction (command bar button) Andibevan Microsoft Access Form Coding 5 15th Feb 2006 05:36 PM
How to define SendMessage once and at each call marshal the arguments SamSpade Microsoft C# .NET 2 30th Mar 2004 07:00 PM
Re: call a sub with arguments from Worksheet_FollowHyperlink event Tom Ogilvy Microsoft Excel Programming 5 28th Aug 2003 10:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:09 PM.