Closing a Userform with Unload Me doesn't work - vba

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.

Related

How do I focus Word document after using userform in VBA?

I have a small userform as interface to various VBA functions. I use these commonly for document automation like inserting tables formatted in certain way etc.
This works great, but I would like to have a workflow where I place the cursor at desired place, click the function, and can continue writing. Currently the form always keeps the focus after being clicked. The form is not modal.
So in image below, I have focus on (1) but want focus on (2) after VBA sub in form has completed.
I did try ending the macro with ActiveDocument.ActiveWindow.SetFocus which does not make a difference.
I could of course close the form after every macro, but I use it commonly, so this would be less preferred.
Please, try one of the two ways. Both of them have to work:
Word.Application.Activate
AppActivate ActiveDocument.ActiveWindow.Caption

Access runs function in control's RowSource when form opened in Design View

I have an Access 2007 form which contains a combo box with the following Rowsource:
SELECT qryProjectsIHaveAccessTo.projID, qryProjectsIHaveAccessTo.projName
FROM qryProjectsIHaveAccessTo
WHERE (((qryProjectsIHaveAccessTo.projSupportTracker)=False));
The query qryProjectsIHaveAccessTo uses a user-defined function as the criteria for one of the columns. This function detects if a Startup routine has been run, and if not runs it.
The problem I have is this: if I SHIFT+open the database and open the form in DESIGN mode, for some reason the user-defined function starts running. This then causes errors as the Startup routine it calls tries to open a form (presumably Access cannot open a form while it is in the process of opening another form in Design mode) and sometimes I am not able to CTRL+BREAK out of it.
The same thing happens when I go to save the form in Design mode.
If I remove the RowSource string from the combo box, this no longer happens. But why would a function which is called in a query included in a control RowSource get fired when the form opens in Design Mode?
Any ideas anyone? Thanks for reading!
When you open a form in design view, Access validates that the form record source is still there, and that all the query fields that are bound to form controls are still there.
If some of these checks fail, Access shows the small green triangle on the bound controls with problems.
While doing this check, it runs the UDF in the query.
As was said in the comments, calling a startup function does very much not belong into UDFs that are called from a query. Put it into a function that is called by an AutoExec macro.

Hide modeless form when all macros stop

First of all I want to thank everyone who has helped me with answers here. You guys have been important in my development and for that you have my respect.
For the question: I want to create a form that displays the status for running multiple macros in sequence. For that i'm showing a form modelees so the macros continue running in the background.
The way I have it setup now, the form remains visible after macros are done.
Is there a way i can trigger the form to hide/unload when no macro is running?
Thanks,
Daniel
Just unload the form at the end of the last macro. I assume you have one calling sub which handles showing the form and calling the macros, so that is where I would also hide the progress form.

Word VBA: Make macro easy to run

I've made a form-letter document with a macro that performs the mail merge. I don't want the user to have run it from the menus, and I want this to be portable. If there's a way for a button to appear on each user's ribbon or quick command menu, I'm not familiar with it.
So I put a button in the document itself. Unfortunately, every form-letter created has the same button in it. I suppose I could write the code to delete every one, but I think that would be slow.
Is there a way to assign a shortcut key to an existing macro, and have it reside in the document?
I had to implement something pretty similar to what you were referring to some 10 staff. My solution (by no means as portable as desired) was to export the macros and forms from my Normal template to the other users, I coupled this with Ribbon customization and it worked well. Unfortunately, when a change was needed, I had to trudge over to everyone's machine individually.
I would suggest you stick with your solution of deleting button after the merge is complete. Here's some code to help with that:
Sub DeleteCommandButton()
For Each o In ActiveDocument.InlineShapes
If o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End Sub
Good luck, hopefully this helps.

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