mrk777
mrk777

Reputation: 167

Calling a Private Sub/Event in Thisworkbook from Module 1 Macro

Hope everyone is doing good.

I have Private Sub Workbook_AfterSave(ByVal Success As Boolean) in Thisworkbook module and I would like to call this from my Module code Sub Mail_it().

So in simple, If I call from Sub Mail_it(), then Private Sub Workbook_AfterSave(ByVal Success As Boolean) should run/execute.

Please help and Thank you in advance.

Upvotes: 0

Views: 2056

Answers (1)

Dan
Dan

Reputation: 660

The Private Sub Workbook_AfterSave(ByVal Success As Boolean) is an event callback function built into Excel. You shouldn't call that subroutine directly.

Instead, make a new subroutine with your function, MyNewSub. Put all the code logic in there. Then call the new subroutine from both Sub Mail_it and Sub Workbook_AfterSave.

EDIT: adding example code to illustrate. The first block is in ThisWorkbook

' Only Excel should call this Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    Call MyNewSub
End Sub

This code is in Module 1

' Your application calls this sub    
Sub Mail_it()
    Call MyNewSub
End Sub

Sub MyNewSub()
    ' Here is where your code goes
End Sub

You can move the subs to different files, or give them different names if you want.

Upvotes: 2

Related Questions