Call Private Sub From Different Module

R

Richard

Hi,

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


Thanks
Richard
 
R

Richard

Sam

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

Regards
Richard
 
S

Sam Wilson

Is there a reason you don't want to have a public sub? There may be another
way round the problem.
 
R

Richard

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

Sam Wilson

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
 
D

Dave Peterson

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
 
R

Rick Rothstein

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
 

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