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

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

Related

Calling Normal Module Sub in Class module in VBA

I have defined a public sub in a common module (normal module). I am trying to access this in class module.
Below is both the code -
Public loadGroupIndicator As Boolean
Public Sub stopLoadGroupIndicator()
loadGroupIndicator = False
End Sub
Public Sub startLoadGroupIndicator()
loadGroupIndicator = True
End Sub
Public Sub getLoadGroupIndicator()
getLoadGroupIndicator = loadGroupIndicator
End Sub
I am trying to use "getLoadGroupIndicator" function in below function of a class module (testClass)
' This is in testClass
' Private sub to execute something on the event
Private Sub m_chckBox_Click()
If getLoadGroupIndicator Then
MsgBox "loadGroupIndicatorLocal is true"
Else
MsgBox "some other msg based on the object property"
End If
End Sub
I am getting the below error msg
Please help me fix this. I am new to VBA and struggling on this since yesterday.
Thanks in advance for any help or relevant pointers.

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

How to set variable to name of pressed command button

I'm trying to write a macro that will have many different buttons. I would like all the buttons to trigger the same function but I need to set a variable in the function to identify which button triggered the macro.
I'm not really sure how I should do this. Is there a way I can say like variableA = ClickedButton()?
Any advice is appreciated
Hi , I think something like this should work
Private Sub CommandButton1_Click()
myfunction "b1"
End Sub
Private Sub CommandButton2_Click()
myfunction "b2"
End Sub
Private Sub CommandButton3_Click()
myfunction "b3"
End Sub
sub myfunction (v as variant)
msgbox v
end sub
Try the following
In the button module:
Sub CommandButton1_Click()
myFunction btn_name:= Me.Name
End Sub
In a normal module:
Public Function myFunction(btn_name As String)
MsgBox "The button pressed was:" & btn_name
'do stuff
End Sub
Doing this, you pass the name of the pressed button (Me.Name) to the function as a variable, so it can be used generally inside that function.

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

VBA Excel Passing an Button as Argument

I want to pass a CommandButton as an argument.
Example:
Sub calc(btn as button)
btn.Caption = "Something"
End Sub
Private Sub CommandButton1_Click()
calc(CommandButton1)
End Sub
Private Sub CommandButton2_Click()
calc(CommandButton2)
End Sub
Is something like the above possible? If yes how can I do it?
edit
Thanks for your response, but I dont get it. So it looks like this now:
Public Sub calc(ByRef btn as Object)
btn.Caption = "Something"
End Sub
Private Sub CommandButton1_Click()
calc(CommandButton1)
End Sub
Private Sub CommandButton2_Click()
calc(CommandButton2)
End Sub
Maybe someone can explain it to me in more detail, because Im very new to VBA.
You need:
Sub calc(btn As MSForms.CommandButton)
btn.Caption = "Something"
End Sub
And you must invoke it following the rules:
calc CommandButton1 // best
call calc (CommandButton1) // ok but verbose
calc (CommandButton1) // type mismatch!
(The type mismatch is because the parentheses evaluate CommandButton1 which results in its default property (a string) which is incompatible with the method argument type)
This is the sub:
Public Sub temp(ByRef cmdb As Object)
cmdb.Caption = "somethine else"
End Sub
This is how you would call it
call sub (commandbutton_1)

Resources