Keep the previous userform open after Unload.Me using Excel VBA - excel

I use the Unload.Me function to close a userform.
I have two userforms, when I close one, the second userform will close automatically as well.
I want try to keep the previous userform open.

A quick workaround, try to specify the exact form you are unloading.
i.e. use Unload UserForm2
If that doesn't help, check if the forms are modal forms. If not, set them to modal.

I tried adding a subsequent instruction in the same sub procedure that called/showed Userform2 (thinking that it might not close Userform1 until it finished the rest of the code after Userform2.show). Instructions like assigning a random value to a random variable did not work, but making a message box pop up did work. For example in the sub procedure calling userform2 you might try:
Userform2.show
Msgbox("Userform2 has already closed. Close this message box to see Userform1.")
This seems despotic to me too, but it worked.

This's caused by a code that executes when the form that you don't want to close becomes the active form.

It's 2022 now, but anyway, for those interested.
You do not need a messagebox, you can just set focus to an item in userform1.
That way you avoid having to click OK to close the messagebox

Related

Userform keeps clearing values when reopened

How do I keep the values that where inputted into the excel Userform from clearing itself out once I click on the finish button? So whenever I call upon the Userform through a Commandbutton the previously filled field information is left the way it is and not to be cleared basically.
This Userform consist of MultiPage which is designed to take in the inputs from the user and place them in excel cells once the user has hit the finish button. Within the Userform contains Commandbuttons which toggles an hide/unhide feature which will also have to be saved somewhere
The Finish page (where the user will close/exit the page) looks like this:
Code for the finish button so far:
I'm not sure why everyone is saying there is no way to do this - you should be able to use:
Private Sub BtnFinish_Click()
Me.Hide
End Sub
And all previous values will be kept. Someone correct me if I'm wrong :).
I should add that once the EXCEL.EXE is closed, the values are lost.

Move focus to a button on a worksheet

I have a worksheet with form control buttons. At the end of a macro called by one button I would like to set the focus to another button, so that if I see that the first button did the job, I could just hit Enter.
I searched the site and got this
ActiveSheet.Shapes("CommandButton1").Select
This selects the shape but Enter does not run it.
Methods of CommandButton do not work.
ActiveSheet.Shapes("CommandButton1").SetFocus
returns the "method not supported" error.
I got this other answer:
Me.CommandButton1.SetFocus
I get "Object needed" error.
This leads me to believe that either CommandButton# object does not exist in Excel VBA or that it refers to a form but my buttons are placed directly on the spreadsheet.
Tried
ActiveSheet.CommandButton1.SetFocus
ActiveSheet.Buttons("Button 1").SetFocus
I found an old sample code with something like this:
CommandButton2.Caption = "CONTINUE"
CommandButton1.Enabled = False
CommandButton3.Enabled = False
This works in that sample, but in that sample the buttons behave differently. I suspect they are ActiveX or something else instead of form control buttons. In that sample right-clicking on them does not bring up the context menu.
I tried
ActiveSheet.Buttons("Button 1").ControlFormat
But do not know what to use next. SetFocus does not work.
With an ActiveX control button named "CommandButton1", you can call
Me.CommandButton1.Activate
from the same worksheet.
On a form control button named "CommandButton1", you can call
UserForm1.CommandButton1.SetFocus
This will make sure the focus is on the CommandButton1.

VBA Excel - Unloading form leads to entire application termination

I am calling a userform A from within another userform B. The problem arises when I try to close the userform B. I don't want to use Hide method, because there are some default values assigned to form properties in the form's Load method - and I would like these values to be reassigned every time the user closes up and opens the form during one session.
So it appears that I should use Unload Me, but the latter stops my entire application as if I used End command.
So the question is, why Unload Me shuts down the entire application, rather than a single form.
Edit: Using Unload showUserFormB instead of Unload Me does not help
Private Sub methodOfUserFormA()
Call FormB.showUserFormB(some_params)
Unload SqlUpload__handlePhantomRows
Msgbox "Why Unload method shuts down entire app?"
End Sub

Userform in Excel 2013 disappears while switching an active workbook behind

I have faced one problem in Excel 2013. In my project, I have a userform that contains a listbox which lists names of all opened excel books.
When a user clicks on a particular name in the list, the appropriate book is activated. So the userform is always on top and while the user is clicking on the names in the list, he can see all opened workbooks getting activated one by one behind the active userform.
This is the way it used to work in Excel 2010.
Now, after upgrade from 2010 to 2013, if a user clicks on a name in the list, selected workbook gets activated but the userform disappears.
What exactly has been broken in Excel 2013 and what is the way to bypass this problem?
Yes, the default behavior has changed. In Pre Excel 2013, The different workbooks are shown within one main Excel window if opened in the same instance. But in Excel 2013, they are shown in their own window. And because of this a modeless userform will only be visible on top of the workbook that was active when the userform was loaded.
And I don't think this has a fix yet. It is by design and may not be fixed.
#Siddharth, based on what you said, I think I have a solution. When you open a form, it will only show up on top of the ActiveWorkbook. As long as all the code is in the same code thread (not sure if terminology is right) , you're stuck with the same workbook; But if you use the Application.Run or Application.Ontime commands it seems to start it's own code thread, which is independent enough to work. Just Activate the workbook/sheet first, and then Unload the form and remote call the macro that showed the form in the first place.
Unfortunately, you need to use Unload instead of Hide, because of the aforementioned issue; which means you need to save any/all options selected.
Private Sub lstFiles_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ThisWorkbook.Application.Workbooks(lstFiles.Text).Activate
Unload Me
Application.Run "USER_Macro1"
'Application.OnTime Now(), "USER_Macro1"
End Sub
 
Private Sub USER_Macro1(Optional Control) ' As IRibbonControl)
ModelessForm.Show vbModeless
End Sub
I tested it in Excel 2013 and so far it even seems to work regardless of the Screenupdating = false issue seen elsewhere.

Edit excel sheet manually while running a code

I would like to know if i can fill in a row of values manually during the run of a code when a msgbox is displayed by the code asking me to fill the same. I tried filling them but after i press 'ok' on the message box, the code continues to run and also, i cant edit the file without pressing ok on the message box. So, could anyone tell me if there is a way I can do that? Thanks in advance!
You could try to use a userform instead of an messagebox. As far as i know there is no simple way to create a messagebox, where you can edit the sheet in background.
If i need to edit the sheet while having an active macro, i'm using a userform. (they are not that hard insert in vba, try it!)
You have to start the userform with the vbmodeless.
Should look like this: (i'm not 100% sure, because i've no vba ide herere to test, but i thinks it's something like this)
Sub ShowMyCreatedUserform()
myUserform.Show vbModeless
End Sub
If this userform is open, your code stops, but you can edit the sheets. After pressing a button (for example "ok"-button) you can continue the macro.
I found 'ontime' command on the net.. I think the code can be split into 2 parts and ontime function can be used..

Resources