PC Review


Reply
Thread Tools Rate Thread

Call Private Sub From Different Module

 
 
Richard
Guest
Posts: n/a
 
      9th Oct 2009
Hi,

Is it possible to run a macro from module1, which calls/runs a private sub
in module2.


Thanks
Richard


 
Reply With Quote
 
 
 
 
Sam Wilson
Guest
Posts: n/a
 
      9th Oct 2009
Pretty sure the answer's no - you'd have to make it a public sub

"Richard" wrote:

> Hi,
>
> Is it possible to run a macro from module1, which calls/runs a private sub
> in module2.
>
>
> Thanks
> Richard
>
>

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      9th Oct 2009
Sam

Not the answer I was hoping for, but thanks anyway. Thought I was missing
something really obvious.

Regards
Richard

"Sam Wilson" wrote:

> Pretty sure the answer's no - you'd have to make it a public sub
>
> "Richard" wrote:
>
> > Hi,
> >
> > Is it possible to run a macro from module1, which calls/runs a private sub
> > in module2.
> >
> >
> > Thanks
> > Richard
> >
> >

 
Reply With Quote
 
Sam Wilson
Guest
Posts: n/a
 
      9th Oct 2009
Is there a reason you don't want to have a public sub? There may be another
way round the problem.


"Richard" wrote:

> Sam
>
> Not the answer I was hoping for, but thanks anyway. Thought I was missing
> something really obvious.
>
> Regards
> Richard
>
> "Sam Wilson" wrote:
>
> > Pretty sure the answer's no - you'd have to make it a public sub
> >
> > "Richard" wrote:
> >
> > > Hi,
> > >
> > > Is it possible to run a macro from module1, which calls/runs a private sub
> > > in module2.
> > >
> > >
> > > Thanks
> > > Richard
> > >
> > >

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      9th Oct 2009
Really it's only to keep everything nice and tidy, so that when another user
has to use the workbook and run a macro there is less chance of the wrong
this being run. I can easily keep the macros within the same module, but
this is a little cumbersome when updating code.

"Sam Wilson" wrote:

> Is there a reason you don't want to have a public sub? There may be another
> way round the problem.
>
>
> "Richard" wrote:
>
> > Sam
> >
> > Not the answer I was hoping for, but thanks anyway. Thought I was missing
> > something really obvious.
> >
> > Regards
> > Richard
> >
> > "Sam Wilson" wrote:
> >
> > > Pretty sure the answer's no - you'd have to make it a public sub
> > >
> > > "Richard" wrote:
> > >
> > > > Hi,
> > > >
> > > > Is it possible to run a macro from module1, which calls/runs a private sub
> > > > in module2.
> > > >
> > > >
> > > > Thanks
> > > > Richard
> > > >
> > > >

 
Reply With Quote
 
Sam Wilson
Guest
Posts: n/a
 
      9th Oct 2009
You could put a dummy parameter in the sub you would rather keep private:

Public Sub test(ByVal dummy As String)

MsgBox "Hello"

End Sub

This won't then be visible to anyone wanting to run a macro from outside the
VBE window, and if you want to call it from another macro you can pass a
dummy parameter:

sub demo

call test("X")

end sub

"Richard" wrote:

> Really it's only to keep everything nice and tidy, so that when another user
> has to use the workbook and run a macro there is less chance of the wrong
> this being run. I can easily keep the macros within the same module, but
> this is a little cumbersome when updating code.
>
> "Sam Wilson" wrote:
>
> > Is there a reason you don't want to have a public sub? There may be another
> > way round the problem.
> >
> >
> > "Richard" wrote:
> >
> > > Sam
> > >
> > > Not the answer I was hoping for, but thanks anyway. Thought I was missing
> > > something really obvious.
> > >
> > > Regards
> > > Richard
> > >
> > > "Sam Wilson" wrote:
> > >
> > > > Pretty sure the answer's no - you'd have to make it a public sub
> > > >
> > > > "Richard" wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > Is it possible to run a macro from module1, which calls/runs a private sub
> > > > > in module2.
> > > > >
> > > > >
> > > > > Thanks
> > > > > Richard
> > > > >
> > > > >

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Oct 2009
In module1:
Option Explicit
Sub testme01()
Application.Run "Testme02"
End Sub

In module2:
Option Explicit
Private Sub testme02()
MsgBox "hey, this is private"
End Sub


Richard wrote:
>
> Hi,
>
> Is it possible to run a macro from module1, which calls/runs a private sub
> in module2.
>
> Thanks
> Richard


--

Dave Peterson
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      9th Oct 2009
Why not make the variable Optional... the Sub will still not appear in the
Macro listing and the OP will not have to pass it anything in order to use
it...

Public Sub TestMe(Optional Dummy As String)
MsgBox "Hello"
End Sub

And then your Demo subroutine can call it like this...

Sub Demo()
Call TestMe
End Sub

or, more simply, like this...

Sub Demo()
TestMe
End Sub

--
Rick (MVP - Excel)


"Sam Wilson" <(E-Mail Removed)> wrote in message
newsD9CF527-DC43-479A-8F79-(E-Mail Removed)...
> You could put a dummy parameter in the sub you would rather keep private:
>
> Public Sub test(ByVal dummy As String)
>
> MsgBox "Hello"
>
> End Sub
>
> This won't then be visible to anyone wanting to run a macro from outside
> the
> VBE window, and if you want to call it from another macro you can pass a
> dummy parameter:
>
> sub demo
>
> call test("X")
>
> end sub
>
> "Richard" wrote:
>
>> Really it's only to keep everything nice and tidy, so that when another
>> user
>> has to use the workbook and run a macro there is less chance of the wrong
>> this being run. I can easily keep the macros within the same module, but
>> this is a little cumbersome when updating code.
>>
>> "Sam Wilson" wrote:
>>
>> > Is there a reason you don't want to have a public sub? There may be
>> > another
>> > way round the problem.
>> >
>> >
>> > "Richard" wrote:
>> >
>> > > Sam
>> > >
>> > > Not the answer I was hoping for, but thanks anyway. Thought I was
>> > > missing
>> > > something really obvious.
>> > >
>> > > Regards
>> > > Richard
>> > >
>> > > "Sam Wilson" wrote:
>> > >
>> > > > Pretty sure the answer's no - you'd have to make it a public sub
>> > > >
>> > > > "Richard" wrote:
>> > > >
>> > > > > Hi,
>> > > > >
>> > > > > Is it possible to run a macro from module1, which calls/runs a
>> > > > > private sub
>> > > > > in module2.
>> > > > >
>> > > > >
>> > > > > Thanks
>> > > > > Richard
>> > > > >
>> > > > >


 
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
How to call a public module macro in private module macro emmanuel Webmaster / Programming 1 13th May 2011 06:34 PM
Improve method of calling a private function in a private module XP Microsoft Excel Programming 1 30th Apr 2008 06:41 PM
How to call a private sub() in another module =?Utf-8?B?SkBZ?= Microsoft Excel Programming 4 13th Jul 2007 08:08 PM
Can't "Call" Error Handler in Standard Module from Form Module =?Utf-8?B?Sm9obiBE?= Microsoft Access VBA Modules 8 21st Apr 2007 07:24 PM
Call Private Function from ThisWorkbook module Bob Phillips Microsoft Excel Programming 3 25th Feb 2004 02:16 PM


Features
 

Advertising
 

Newsgroups
 


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