How to close form without closing the whole Excel? - vba

I've created a Form in excel 2017 with a "Cancel" button, but I don't know how to code it so that it closes itself when clicked. I've found things like "Application.Quit" but it closes everything.

There are two ways to achieve that. Both will only close your form and keep Excel opened.
I've asked a question about it a while ago maybe this can help you.
1. Hide Method
Your userForm has a Hide method, you can call it to hide your form.
Example:
Private Sub btnCancel_Click()
yourFormName.Hide
End Sub
Using the Hide method will only hide the form, it will be totally closed when you close your Excel file.
If you make changes in the form, hide it and then show it again all changes will be kept (ex: change the value of a textbox before hiding it and it will stay the next time you show your form).
The Activate event won't be triggered next time you show the form since the form was still active but hidden.
2. Unload Method
You can call the Unload method to unload your form.
Example:
Private Sub btnCancel_Click()
Unload me
End Sub
The form is unloaded from the memory. If you make changes in your form and then unload it, it won't be kept next time you show your form unlike with the Hide method.

You can use Unload Me to close the Userform.

Related

How can you view Word documents while macro is running?

I want the user to be able to review the word document that the macro generates before stopping the macro. This way, they don't have to enter all the data again, since, when the macro stops, all the data is erased. There are a lot of fields that I don't want the user to have to re-enter if something is wrong.
I've tried activating the macro in a couple different ways, from a button on a word document to starting it from VBA Alt+F11, but I get the same result: The user cannot edit or view the word document while the macro is running, even though it isn't doing anything.
'''
Private Sub CommandButton1_Click()
InputForm.Show
End Sub
'''
The user is not able to view the word document until the macro is stopped and all the data that was input is erased. Alternatively, if it is possible to save the data that has been input so that it is kept for next time the program runs, that would work since I have a "Clear All" button.
Somewhere, you have a subroutine that does something like this:
Sub foo()
UserForm1.Show
End Sub
You will need to change that to display the form vbModeless, in order to allow the user to interact with the Document. Note that this will allow the user to interact with the Document while the form is displayed.
Sub foo()
Dim uf As New UserForm1
uf.Show vbModeless
End Sub
If there is any additional executable code after the uf.Show, it may need to be refactored as well, as it will execute immediately after the form is Shown.
If you don't want the user to be able to interact until after the form does whatever it does, then do not use the vbModeless option, and add a QueryClose event handler to the form. This way we ask the user if they want to review; if they say "Yes", then we hide & re-display the form with the vbModeless option. Now, they'll have the form open and the document will be editable.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If MsgBox("Review document?", vbYesNo) = vbYes Then
Cancel = 1
Me.Hide
Me.Show vbModeless
End If
End Sub
Of course, that may not be quite what you want, either. But those finer points can be worked out separately. There's a lot of things I don't know about your use-case, which can almost certainly be accommodated with proper use of the UserForm. Do note that I can't entertain too many tangentially related follow-up questions, but this should be enough to get you started.
I'm not sure it's possible to persist values in the form beyond it's Terminate event which will happen when the user closes the form or if any code invokes the Unload <userform object>. It would certainly be possible to serialize that data into a text file, or persist it somehow within the Document's custom properties/customer data properties, but that should probably be a separate question.

Hide userform in VBA in order to work with data

I have a following problem. I would like to hide a userform in order to work with data in worksheet.
I would like to enable user to get back to application.
So I thought if there is a possibility to move userform down on the screen and activate workbook.
Afterwards the user could drag userform back in the middle of the screen and work with it again.
Does anyone have an experience with something like that?
You can use me.hide to hide userform, retaining all values entered into the form. Use me.show to bring back the userform to the screen.
This is different from unload me, which unload the userform from memory, destroying all values entered. Calling the form again will start the form a new.
If you want the form to be still visible and allow user to interact with worksheet behind, then set the ShowModal property to False.. The default is True.

Progress Bar in Excel using VBA and userForms

so I've been looking and cant find anything on this, I hope someone can help.
what I am trying to do:
I want to create a progress bar for my excel workbook that I am making. in order to accomplish this I have created a user form with data fields that I can manipulate from outside of the userform. what I would like to do is to load the userform from inside a module and then from the same module update the userform as the module continues to run.
is there a way to do this?
currently when I use userForm1.Show it displays the userform, but the control never goes back to the calling module, and the code ends when reaching the End Sub for Private Sub userForm1_Activate()
any help would be appreciated.
thank you
You are opening the userform as a modal form. To return the control back to the calling module, you must open the userform modelessly:
UserForm1.Show vbModeless

Program is running even after the close button pressed

I have a 2 form program,the first form's button click will open the second form when clicked. I tried using a .close() to hide the first form, but that ended the whole program. Then I tried using a .hide(). Now, when I click the X button in the top right of the second form, the program closes, but is still running in the background. How do I get around this?
By default, a VB.NET application will exit when you close the startup form. If you're hiding the startup form and closing the second form then the startup form is still open, so the app won't exit.
If you want to be able to close the first form without exiting the application then go into the project properties and set the shutdown mode to when all forms close instead of when the startup form closes. That way, you can show your second form and then close your first form. Closing the second form will then exit the app.
Simply close the main Form from inside the second form's FormClosed event.
Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs)
Form1.Close()
End Sub
You should use Me.Close() just before the ShowDialog() function for the second window.
if you want the 1st form to be visible again, add a form1.show() to the form_closing event of the 2nd form
if you want the program to end, add an Application.Exit to the form_closing event of the 2nd form

VB.NET: How to close and re-open a dialog in this case?

I'm developing a WinForms app in VB.NET, that handles sets of style data, and when the user clicks on another set's label, it prompts through a dialog "You are leaving this style preset to edit another one. keep changes on this one? [Yes] [No]"
But, I'm facing the problem that, when the user clicks either option, and the dialog closes, everything has to be refreshed, and loading the form again seems a good option.
I've tried putting a public sub on a module, that does this:
Public Sub CloseOpenStyleDlg()
KeepOrDiscardPrompt.Close()
StylesDlg.Close()
StylesDlg.ShowDialog()
End Sub
But as soon as that sub is called from the prompt, it crashes the application. (doesn't show an error in debug, simply crashes) How should I, from a given dialog, close the dialog, it's parent, and re-open it's parent? (which triggers all the Dialog_Load() code of the parent)
Thanks in advance! :)
You need to instantiate the dialog again. If I take your code for example:
Public Sub CloseOpenStyleDlg()
KeepOrDiscardPrompt.Close()
StylesDlg.Close()
StylesDlg = new StylesDlg()
StylesDlg.ShowDialog()
End Sub
When a form is closed, all resources created within the object are closed and the form is disposed.
If you want to reuse the Window instance use StylesDialog.Hide() function instead.