Progress Bar in Excel using VBA and userForms - vba

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

Related

How to close form without closing the whole Excel?

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.

How to avoid resetting global variables set from userform for use in another module?

I have a userform in Excel with lots of options to select and values to enter. In the code for the userform I have several Public variables declared that store the info entered in the userform. When the user is done entering everything in the userform, they click the 'Process' button and the main function of the form is carried out (compiling data from several different Excel files into this sheet). After all the data is compiled, I have another command button on the first worksheet for running a summary report of the compiled data, which is put in another worksheet in the same workbook. The code for this summary macro is defined in Module 1 rather than in the code for the userform, since it is connected to a different command button. It seems that all my Public variables that are set in the code for the userform are reset when I run this summary macro, so nothing is working correctly.
Do I have to somehow link this module to the userform to be able to access those public variables? Or do I have to put the code for the summary macro in with the code for the userform? If that's the case, how do I call a Sub from the code for the userform in the code for the RunSummary_Click() command button code?
I got it to work by running the userform as modeless (ShowModal = False) and declaring the Public Dim's of interest in the Module linked to the userform rather than in the userform code itself. Thanks for the help everyone.

Clicking away from userform open text box VBA Excel

I am not very good with userforms so hopefully this is an easy question.
I have a userform in a spreadsheet that opens with
UserForm1.Show False
To ensure excel can still be used while it is open. What I would like to happen is that whenever you click away from (but not close) the userform a text box is opened asking if you want to maintain the edited values or not. I can't figure out how to initiate more code when the userform is no long the point of interest. Usually items on userforms have enter and exit which I believe would do what I need but I can't find the equivalent for the actual userform.
Any advice would be appreciated.
To allow a user to enter values into excel while a form is visible, you will need to update the ShowModal property on the userform to false.
Or as you are showing the userform, you can show it vbModeless.
UserForm.Show vbModeless

View Code When Modal Dialog Is Open In Excel

This may be a stupid question, but I feel like I'm in a pickle. I have a modal UserForm that opens when an Excel workbook is opened. When the UserForm is closed, the Excel workbook is saved and closed. I need to be able to view my code, but I can't seem to figure out how to do that because if I close the modal dialog box, the workbook closes. Does anyone know how I can view my code? I really apologize if this is a stupid question, but I can't seem to figure it out.
Thanks for you time and effort.
Without restarting the workbook i.e when the userform is shown in modal, you can use CTRL + Shift + Pause/Break to get into the VBE
Depending on laptops the key combination might change. Here is another which you can try.
Fn + Pause/Break
Hold the shift key when opening the workbook. This allows you to open office applications with macros not running and can be useful in situations like this.
Then view the macros (hit Alt+F11 to open it this editor).
The two other suggestions are good. For easiest debugging, I'd put the code that opens the userform in a separate routine and then call that routine from Workbook_Open. That way you can run and debug your code without having to re-open the workbook.
Then your ThisWorkbook module might look something like this:
Private Sub Workbook_Open()
MyUserformProcedure
End Sub
Sub MyUserformProcedure()
UserForm1.Show
End Sub
You could then comment out the line in Workbook_Open and call MyUserformProcedure, and uncomment the line when you're finished debugging.

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.