Call to a different macro at a certain line - vba

Is it possible to use the call feature to call a macro at a specific line? I ask as the macro i am coding does this - First it runs half of the code, then based upon user choices it opens up a custom UserForm, and runs the UserForm command buttons. After this I need a command button to go back to the code the line after the UserForm was used. The UserForm works as intended and many different stats calculations can be run. It has a button that returns the user back to the rest of the code, but I can't get that to work.
I have tried using both GoTo statements and the Call feature (The most promising solution) but have had no success so far.

There are several ways to achieve it, I will show one of them.
You can modify your procedure (macro) asking for a value as optional.
Sub MyMacro(Optional Answer As Boolean)
If Answer = True then
'Do some stuff
Else
'Do some stuff
End If
End Sub
As Answer is optional you can call your Macro with or without arguments.
Suppose you call your Macro from your user form:
Call MyMacro(True)
Hope this give you some hints.

You can use VBA for this, but I would recommend using the Windows Task Scheduler. I think that will be a lot better, all things considered.
https://www.digitalcitizen.life/how-create-task-basic-task-wizard

Related

Vba Macro works but not starting in main()

Have created a Macro - multiple subs, functions and forms for Solidworks.
I'm sure the code is dubious but it works when I force it to start in main()
When I add a button in Solidworks to start the macro it defaults to a different sub, which appears to be alphabetical, I get a similar behaviour starting the macro from the editor.
It appears all the subs listed are the ones with no arguments passed in.
Could anyone please guide me on why this happens? I'm sure I could frig a way around by renaming the subs, but don't realy want to.
It appears I was to impatient, again!
When adding the macro button in Solidworks you are given the choice of choosing the sub to run ... as Method

VBA to stop running nested macros in excel?

I have a button on my spreadsheet that when clicked, triggers a series of nested subs. Example:
Sub Button1_Click()
Macro1
Macro2
Macro3
Macro4
Macro5
Macro6
End Sub
Because the time to run all these subroutines can be rather lengthy, and the user cannot do anything else in excel while it is running, I was wondering if there was a way to create a button that, when clicked, can halt the running of all these subs? I've seen recommendations of adding DoEvent to the code, however, I'm not sure where in my code to implement it.
Any insight would be much appreciated.
The DoEvents statement will allow any lagging user input and handling of events.
The Solution
What you probably want to do is overload the OnKey event handler and use it to set the value of a global variable. Next in your code above you need to verify if that variable is set. If it is abort execution. Be sure to add DoEvents statements in any moments in your code where you want this to run.
Optionally, w/o the OnKey handler, DoEvents will allow the user to hit ESC to stop the execution of your macro. But it will revert to debug mode and highlight the current executed row in your procedure. Not sure this is what you want however.
Tip - what to do first
Having said that... what I personally think you should do FIRST is optimize your VBA code. I would assume there is probably a lot of room for improving the execution time of your procedure (my tutorial).
You can create Macro to perform below steps/operation and call when you want to abort the other running macros:
Step 1-- Menu bar -> Run -> Reset
Step 2-- Menu bar -> Run -> Break

DoubleClick captured in my UserForm

As I bet I can tell the following is happening in my Excel 2013 macro:
Sub WorkSheet_BeforeDoubleClick is calling a form
Some logic populates and shows the form
The double click event is captured by the form as an undesired selection in a listbox
Is there a correct way to suppress the double click in the middle of the subroutine? I have ideas for getting around it (sleeping the thread works, re-positioning the form works, locking and unlocking the listbox based on other events works) Those are all poor workarounds and I am hoping for a built-in solution that I am just not aware of.
(And setting Cancel = True does not work because I believe setting only kicks in once the code is finished running. The double click becomes a selection in the middle of the subroutines)
If the issue is the form you are launching from the macro is catching the same double click event try the following:
1) Have your macro call a new sub routine via 'Application.OnTime()' schedule it to run just long enough in the future to pass the 'physical click', maybe 1s. Instead of directly calling Form.Show(), as you are currently doing.
2) Your new subroutine just has to do: Form.Show(), and whatever else you deem necessary.

Input values into an Input Box that was called by another Macro

I am writing a VBA macro that calls some macros from other Excel workbooks. These Macros are protected and I do not have the ability to see or modify their code. In one of the macros, an InputBox is called.
Is there a way to automatically trigger the OK button so that the InputBox does not load up (or pops up and then closes without prompting the user)? I can live with the default value for the input box to be used (though I'd also like to modify it if possible).
Please let me know if more information is needed - thanks in advance.
Have a look at example:
Sub MyMacro()
Call MacroInDifferentWorkbook
'here code to catch InputBox
End Sub
If you're trying to do that in that way, the answer is NO, you can't catch InputBox from MacroInDifferentWorkbook. It was well explained here:
Simulating Multithreading in VBA using Excel
Multithreaded VBA – An Approach To Processing Using VBScript

Closing a Userform with Unload Me doesn't work

I need to close an Excel userform using VBA when a user has clicked a submit button and operations have been carried out.
How can I close a Userform from itself?
I have tried this but it returns a 361 error.
Unload Me
As specified by the top answer, I used the following in the code behind the button control.
Private Sub btnClose_Click()
Unload Me
End Sub
In doing so, it will not attempt to unload a control, but rather will unload the user form where the button control resides. The "Me" keyword refers to the user form object even when called from a control on the user form. If you are getting errors with this technique, there are a couple of possible reasons.
You could be entering the code in the wrong place (such as a
separate module)
You might be using an older version of Office. I'm using Office 2013. I've noticed that VBA changes over time.
From my experience, the use of the the DoCmd.... method is more specific to the macro features in MS Access, but not commonly used in Excel VBA.
Under normal (out of the box) conditions, the code above should work just fine.
Without seeing your full code, this is impossible to answer with any certainty. The error usually occurs when you are trying to unload a control rather than the form.
Make sure that you don't have the "me" in brackets.
Also if you can post the full code for the userform it would help massively.
Unload Me only works when its called from userform self. If you want to close a form from another module code (or userform), you need to use the Unload function + userformtoclose name.
I hope its helps
It should also be noted that if you have buttons grouped together on your user form that it can link it to a different button in the group despite the one you intended being clicked.