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

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.

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.

Related

How do I call a module from my userform in vba?

I have trying to call a module by assigning my sub name however it does not work
I tried by
Private Sub Filtert04_click()
Call filter 'the sub name
End Sub
It would be great if someone could help thx
Make sure the Sub in the module is defined as Public
Public Sub DoIt()
MsgBox "Hi"
End Sub
and then from your userform, you can call it like this:
Private Sub Filtert04_click()
DoIt
End Sub

How to call DblClick sub from code VBA Excel

I have an Excel worksheet with two forms: Form1 and Form2.
Form1 has TextBox1 with double-click events:
Public Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' Some activity...
End Sub
Form2 has CommandButton2 with a click event:
Private Sub CommandButton2_Click()
' Another activity...
End Sub
I need to call the TextBox1_DblClick sub from the CommandButton2_Click sub.
How can I make a call like this?
Create a new module, add to it the sub with the operations you wish to perform
Sub mySubroutine()
' Do stuff
End Sub
Then call this from both of your click handlers
' In Form1
Public Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
mySubroutine
End Sub
' In Form2
Private Sub CommandButton2_Click()
mySubroutine
End Sub
If you wanted to pass stuff (like text box strings etc) from your forms, then include input arguments in your sub
Sub mySubroutine(str As String, n As Long) ' Passing arguments
' Do stuff
End Sub
Then pass those arguments when you make the call from anywhere
' Get a String and Long from data on the form, then call mySubroutine
mySubroutine str:=myString, n:=myLong

VBA: call Forms in sub

I have a big problem with getting called a UserForm from a sub.
This is a part of my code:
Sub TestForArray(ObjectName,FormName As String)
Forms(FormName).Controls(ObjectName).List = NewArr
End sub
when I call this sub in a Private Sub, like this
Private Sub UserForm_Initialize()
Call TestForArray("Form1", "ComboBox1")
End Sub
i get the error; "Sub or Function not defined" and higlights the word Forms
what am I doing wrong?
If the goal is to have one Sub that can be called from different UserForms to initialize the UserForm's Controls, you can give the UserForm itself as a parameter.
Example:
Sub TestForArray(oForm As UserForm, sObjectName As String)
aNewArr = [{1,2,3,4,5,6}]
oForm.Controls(sObjectName).List = aNewArr
End Sub
and:
Private Sub UserForm_Initialize()
Call TestForArray(Me, "ComboBox1")
End Sub
Greetings
Axel

Excel VBA: Autorun when Create New Sheet

I want to autorun a macro whenever I create a new worksheet in a workbook. Below is what I tried:
Private Sub Workbook_NewSheet()
Call Macro1
End Sub
Sub Macro1()
...
...
End Sub
Turns out nothing happened. Why?
I guess you have put them under the wrong place.
Private Sub Workbook_NewSheet(ByVal Sh as Object)
Call Macro1
End Sub
should be put under ThisWorkBook while
Sub Macro1()
...
...
End Sub
should be put under Modules

How to make an argument optional?

In a code Module (named ribbon)
Sub Form1Show(control As IRibbonControl) ' this is custom ribbon
Form1.Show ' works
End Sub
In ThisWorkbook module:
Private Sub Workbook_Open()
Call Form1.Show ' error: argument not optional
End Sub
Can somebody explain this?
Declare the parameter as optional; otherwise, it is required.
Sub Form1Show(Optional control As IRibbonControl)

Resources