Error calling macro from userform - vba

I have a userform to call a macro in a separate module when a button is clicked. I get the following error: "Run-time error '450': Wrong number of arguments or invalid property assignment"
In troubleshooting I removed the arguments and changed the dummy macro I was calling to not take arguments, but I get the same error.
Public Sub btnSubmit_Click()
Dim Description As String
Dim Priority As String
If (checkCleared.Value = False) Then
MsgBox ("Please certify that all sensitive informationhas been removed and then submit")
Exit Sub
Else
'Description = formScreen.txtDesc.Value
'Priority = formScreen.comboPriority.Value
'Application.Run ThisOutlookSession!postScreenedEmail(Priority, Description)
Application.Run ThisOutlookSession!postScreenedEmail
End If
End Sub
In the separate module:
Public Sub postScreenedEmail() '(Priority As String, Description As String)
MsgBox ("postScreened")
'MsgBox ("Priority is: " & Priority & " and Description is " & Description)
End Sub
I have tried other methods of calling the macro such as "Call postScreenedEmail()" but it cant see the macro then. My end goal is just to grab values from the userform and pass them to the other macro so they can be used with the API I am working with.
Edit: I may have mixed my terminology, this is the hierarchy I am working with (can't post pic with my rep). That being said I tried to do the call with just Application.Run "postScreenedEmail", Priority, Description and it changed nothing
-Project1(VbaProject.OTM)
-Microsoft Outlook Objects
| ThisOutlookSession
-Forms
| formScreen
|
-Modules
Module1

Try:
call postScreenedEmail
instead of:
Application.Run ThisOutlookSession!postScreenedEmail
Since your sub is public, vba should be able to find it without the module reference.
If this works, add the reference again (makes your code more readable, especially for others, as ckuhn203 pointed out in the comments) and see if it breaks. If so, that's where the problem is.
EDIT:
Are you sure you're referencing the right module?
If I try:
-Project1(VbaProject.OTM)
-Microsoft Outlook Objects
| ThisOutlookSession
-Modules
| Module1
in Module1:
Sub jzz()
Debug.Print "test"
End Sub
and in ThisOutlookSession:
Sub test()
Call Module1.jzz
End Sub
it works. No problem. Using:
Application.Run Module1.jzz
instead of Call trows a compile error.
Even:
Sub test2()
Call ThisOutlookSession.test
End Sub
from Module1 works, without problems.
Can you run such small tests to try to get the references right?

Try this... Application.Run takes a string for procedure name, and then comma-separated list of parameters/arguments:
Application.Run "Procedure_Name", arg1, arg2, arg3
So I think this should work:
Application.Run "ThisOutlookSession!postScreenedEmail", Priority, Description

Related

VBA Code - Call a macro and stop the run until certain lines

I have a Macro (the name is Test() ) from the Module1, and normally to call this Macro, i am using the code :
Call Module1.Test
But i am looking to a VBA code to call my Macro Test from Module1 until certain lines of code
Example stop the runing when the macro is arriving to the line 900
Thanks for your help
I'm not sure, but I can guess that you might want to run the Test() subroutine in different parts:
Sub Test(Optional stop_at_900 = False)
Debug.Print vbTab & "do the first part of the code"
'other code
900: If stop_at_900 Then Exit Sub ' 900 label is not needed, it is placed here for labeling
Debug.Print vbTab & "do the rest of the code"
'other code
End Sub
Sub UsageExample()
Debug.Print "Doing full Test() code..." ' usual (full) use
Call Test
Debug.Print "Doing Test() code until 900 line..." '
Call Test(stop_at_900:=True) 'partial use
End Sub

Print Button In Excel 2016 Using Macros?

I'm currently trying to create a Print Button on one of my worksheets. I need it to print that worksheet as well as another one. Both of the names are "Budget Sheet" and "Listed Commitments Sheet" without the quotation marks.
I created the button without hassle, but I know very little about Macros so I still need the code. I've tried multiple solutions, but nothing seems to work. I've recently tried to use this Code, but it hasn't worked. What am I doing wrong? What code could I possibly use instead?
Private Sub CommandButton1_Click()
Function PrintMultipleSheets()
Sheets(Array("Budget Sheet", "Listed Commitments Sheet")).PrintOut
End Function
End Sub
It comes up with an error that says "Compile Error: Expected End Sub".
The code doesn't compile, because you can't have a Function inside a Sub.
Get ride of the line Function PrintMultipleSheets() and get rid of End Function. It should work I think. You'll end up with:
Private Sub CommandButton1_Click()
Sheets(Array("Budget Sheet", "Listed Commitments Sheet")).PrintOut
End Sub
Just a simple loop with a print call:
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call printSheet(ws)
Next
End Sub
Function pasteContents(ws as Worksheet)
ActiveSheet.PrintOut
End Function

Excel VBA to run after spreadsheet is refreshed

I've read lots of other posts (particularly this) and even tried to replicate a few and I don't understand what's wrong.
I'm trying to get a function to run after the spreadsheet has been refreshed. It seems that I have to mess around with query tables, though if there is some way that I can avoid that, please tell me. I've attached what I think are the relevant code snippets (though I could have missed some).
The error I get is runtime eror: '9' Subscript out of range
code in "this workbook"
Dim qtevent As qtClass
Private Sub Workbook_Open()
Set qtevent = New qtClass
Set qtevent.HookedTable = ThisWorkbook.Worksheets("REFRESH SHEET").ListObjects(1).QueryTable
MsgBox "Qt Events Run"
End Sub
Code in class qtClass
Option Explicit
Private WithEvents qt As Excel.QueryTable
Public Property Set HookedTable(q As Excel.QueryTable)
Set qt = q
End Property
Private Sub qt_AfterRefresh(ByVal Success As Boolean)
MsgBox "qt_AfterRefresh called sucessfully."
If Success = True Then
MsgBox "If called succesfully."
End If
End Sub

Insert text string through other macro with Excel VBA

I'm having an issue and dont know if this is possible to be done.
I need to insert a string variable through another macro that is protected and can't access the VBA code.
At my code, I intend to send a string when call the protected macro (with application.run "macroname"). This protected macro prompts a selection window to select a file. I pretend to automatically insert at macro's prompt my string and send "ENTER" command to open the desire file.
Is this possible to be done?
If I understood you correctly, this is what you are looking for:
1) Locked workbook (VBA module is protected) contains e.g. the following macro:
Public Sub Test(sInput As String)
ActiveCell.Value = sInput
MsgBox sInput
End Sub
What you are trying to do, is to call this Test macro, but you don't know how to pass parameter (correct me if I got this wrong).
This is how you do it:
Sub RunProtectedMacro()
Dim vResult As Variant
vResult = Application.Run("'" & LockedWorkbook.Name & "'!Test", "some string argument")
End Sub

Call a VBA sub using a string value [duplicate]

This question already has an answer here:
Dynamic Function Calls in Excel VBA
(1 answer)
Closed 7 years ago.
This is my test code
Sub dotask()
Dim qusub As String
qusub = Worksheets("Task List").Range("C2").Value
MsgBox qusub
Application.Run qusub
End Sub
Sub msg1()
MsgBox "sub msg1"
End Sub
Sub msg2()
MsgBox "sub msg2"
End Sub
Sub msg3()
MsgBox "sub msg3"
End Sub
Sub msg4()
MsgBox "sub msg4"
End Sub
All of this is contained in a single standard module. I've read Trying to call a Sub with a String - VBA and wrote my code according to what I found there (i.e. using Application.Run). Cell C2 of Task List worksheet contains "msg3" at the moment. When I execute sub "dotask" I first get a message box saying "msg3" as I want but then I get the following error message:
Run-time error '1004':
Cannot run the macro 'msg3'. The macro may not be available in this workbook or all macros may be disabled.
I'm working on Excel 2010 and the file is .xlsm - what should I do to get my code to execute as I want it?
just ran it over here. msg1 seems to be a reserved word... change it to something else and it works fine =)
Using GetRef, you give the reference to the sub.
See my question here for example
EDIT: following the suggestions in the comments, here part of the solution to this question.
sub one(para)
WScript.Echo para & " from one"
end sub
sub two(para)
WScript.Echo para & " from two"
end sub
sub main(subname, para)
Dim f : Set f = GetRef(subname)
f para
end sub
main "one", "test" '=>test from one