so basically I need to pause the macro code, wait for user input and then continue the rest of the code. I know that by using the "modeless userform", this can be done.
But the point now is that all the code after the userform popped up needed to written in the command button part (basically it is the userform own module). And because of that, all the initialization, all the variables that I still need has been wiped off.
So I am asking is there a way to pause in the middle of a vba code then, wait for user input, then continue the rest of the code
Thank you very much for your help
What do you mean as "user input"?
Standard input methods as MsgBox, InputBox or modal user forms do what you are asking;
"code after the userform popped up needed to"...
Instead of closing a form, hide it. Code continues and form data
still available;
Now, if you mean by "user input", manipulating workbook, you must go on events:
Example: Create a before user input macro and then place remaining code in a Worksheet_Change event.
Related
i want to run my code such that it can pause and allow user to filter a column and then the code continues to run. Is this possible?
I thought of using inputbox to prompt user to key in the name. However, this is not feasible because user may not be able to remember the exact name. It will be more convenient for the user to look at the drop down list and select the name he wants to filter.
Please advice me how can this be done. Thank you
You can't stop a VBA macro to wait until an action was done within Excel.
I see the following possibilities:
a) Create a small userform showing a dropdown. If you show that form modal (that is the default), the macro will pause and wait until the form is closed. However, you will need to implement code to fill up the dropdown and to set the filter by code.
b) End your macro and implement an event that fires when the user changes the filter. There is no "On filter change"-event in Excel, but there is a work around: Excel VBA Filter Change event handler. Put the second part of your code into a separate sub and call that from the event handler.
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.
I have a VBA macro, developed by a third party, and which is password-protected. When the macro finishes what is is supposed to do, it shows a message box, and to continue beyond that the user needs to click a button. Before the button is clicked, you cannot do anything else .
I need to run that macro on several files. Is there a way to bypass the message box, and just call another instance of the macro, on a different file...?
Hi I have a VBA application with a combobox selection option.One of the combobox option is Other which when user select that option an invisibled label will pops up and prompt the user to fill the txtOther control.now my question is how I can get rid of the prompted label and make it to invisible after user fill the txtOther and move(focused)in other control?
here is a shot of my app:
Thanks for your time and help
It depends on how often or when you want the check to be done. I couldn't find a good reference to show you all of this, but you can view your VBE.
Within the code behind your userform you can use events for the text box.
If you want the check done every time you leave the text box:
Use the TextBox_Exit event.
If you want it to fire whenever the contents are changed, used TextBox_Change. There's lots of options, but based on your explanation I'd probably use Exit.
This answer shows some syntax examples for using the Textbox_Exit event.
I would like to have a VBA macro pause its execution and wait for the user to press a key before continuing. Is there a quick way to accomplish this, or am I better off using a window popup?
A warning: In my experience, unless your VBA specifically includes DoEvents statements, Office applications are unresponsive while VBA is running.
If you only want to interrupt a process temporarily until the user is ready, then using a pop-up is by far the simplest method.
Adding something like the following would have the desired result:
Msgbox "Click to go on", , "Example"
The user would click to continue or could hit enter or space.
If you wanted a more specific pop-up, loading a modal userform (in Excel) or a modal form (in Access) from code will halt the suspend the calling code until the form is closed.
Honestly, I would resist including a pause unless it's absolutely necessary. Any unnecessary interuption is an annoyance and hinderance to efficiency. If you simply want to inform the user about what is happening you can update the screen to explain what the code is doing as it performs actions.