Calling a Private Sub

colosgps1975

New Member
Joined
May 30, 2002
Messages
49
Should be an easy one...

If I have a 'Private Sub', can I call it from another sub?

example

Private Sub DoThis()
Range("A1").value=3
End Sub

Sub Macro1()
Range("A2").value=1
DoThis
End Sub

How can I make the private sub work from the other sub?

Thanks for the help.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Only if your second macro is in the same module!! Why a Private Sub?

lenze
 
Upvote 0
If these are in the same module then it should work fine? What is not working about what you have?
 
Upvote 0
If they are both in the same module then the code you posted will run as is.


What modules are they in relative to each other?
If they are in different modules you can use Application.Run
Code:
Sub Macro1()
Range("A2").Value = 1
Application.Run "DoThis"
End Sub

Or, why not just make the Sub public?
Code:
Option Private Module
 
Sub DoThis()
Range("A1").Value = 3
End Sub
 
Upvote 0
Subs are private to the module in which they reside. If all of your code is in a worksheet module, then all the subs in that module can access each other. If your private sub is in a user module that you inserted, and you are trying to access it from a worksheet module, that won't work.

So assuming the two subs are in the same module, your posted code should work fine.

Does your other sub need to be private? For basic macro stuff it really shouldn't matter that much...
 
Upvote 0
If you add Option Private Module at the head of your module code, all of your procedures can be public but will not show in the macro list in Excel.
 
Upvote 0

Forum statistics

Threads
1,203,485
Messages
6,055,688
Members
444,807
Latest member
RustyExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top