How to close a userform and open another in VBA - vba

I have one userform in the workbook "main.xlsm" that opens upon opening the workbook, on this userform i have one button that opens another workbook "test1.xlsm". In the "test1.xlsm" workbook i have a userform that opens upon opening the workbook, on this userform i have one button that closes this workbook and saves it. The idea was that after closing the userform from "test1.xlsm" i should get back to the userfom from "main.xlsm", but on the line Workbooks("test1.xlsm").Close it closes all the opened userforms
this is the code for the button from the userform in "main.xlsm" :
Private Sub CommandButton1_Click()
Workbooks.Open ("D:\test1.xlsm")
End Sub
this is the code for the button from the userform in "main.xlsm" :
Private Sub SaveButton1_Click()
Workbooks("D:\test1.xlsm").Close SaveChanges:=True
End Sub

After asking this question on an excel forum i got the suggestion to change the ShowModal property for each form to ShowModal FALSE. So i added the this argument to my code in ThisWorkbook:
UserForm1.Show vbModeless
in both userforms and it worked.

Related

vba - why does userform come up twice after closing thisworkbook

I created a simple userform for testing, but one issue comes up and i have no idea how to resolve it.
This is the case:
When I open .xlsm file for the first time and close the userform and workbook, this closes both of them.
But when I continue to click the .xlsm file again, close the userform and the workbook, it (the userform) pops up again.
It seems the workbook doesn't close.
Then i need to close the userform again to close this workbook.
I would like help to understand why the userform comes up again?
The userform only has one listbox controls.
The following code is in a module:
Private Sub Auto_Open()
UserForm1.Show
MsgBox "Close2"
ThisWorkbook.Close
End Sub
The userform contains the following code:
Private Sub UserForm_Initialize()
Application.Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "Close1"
End Sub
You could try to put this into the workbook code section as followed.
And not put it in a separate module.
Private Sub Workbook_Open()
Application.Visible = False
userform1.Show
MsgBox "Close2"
ThisWorkbook.Close
End Sub
you can remove the code for the user form initialize.
The thing is as its hidden you might open it twice cause you do cause it does not respond directly.

Command button to return user back to source workbook

We have a userform that pops-up and remains on top of all applications.
Since users are always working on multiple workbooks and applications, I want a button on the userform. On clicking this button the users should be returned to the workbook from where the userform is activated.
Let's say the userform is activated from Book1. On clicking the button, users should be returned to book1.
I'm just an excel beginner. Any help will be appreciated
Something like this should work in the code for the userform:
Public wbSource As Workbook
Private Sub CommandButton1_Click()
wbSource.Activate
End Sub
Private Sub UserForm_Initialize()
Set wbSource = ThisWorkbook
End Sub

automatically close excel userform when change sheet

I have a userform that automatically appears when a new sheet is created from a pivot table drilldown. I am trying to figure out how to have this userform automically close if the user navigates to any other sheet in the workbook.
You can use the SheetActivate event of the Workbook object to check the name of the worksheet that is active and take action accordingly. For example:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet1" And UserForm1.Visible Then
Unload UserForm1
End If
End Sub
This event is available in the ThisWorkbook module of your Excel Workbook. I believe your UserForm will need to be Modeless for users to be able to change sheets while the form is visible.
You can read about the Workbook.SheetActivate Event here.

Userform not triggering Initialize or Activate event

I kept a userform control button in my worksheet to fire up a macro, which in turn shows a user form, In the form I wish to display the opened files in checkboxes(using the Workbooks collection).I wish to run a macro that performs action for the user selected files only.
So for the button in my worksheet, I have assigned the following macro
Private Sub Button2_Click()
Load MyForm
MyForm.Show
End Sub
At first I kept the below code in the module where my macro sub is there.Since it's not working, I right clicked on user form and selected view code and kept the below code there.But still it's showing the same static designed user form, not the dynamic.I kept breakpoint at both load Myform and MYform.Show() and I stepped through code.It never went into intialize or activate method at all.
Private Sub MyForm_Activate()
'for checking the whether this method is called or not I am trying to change caption
MyForm.LabelSelectFile.Caption = "dhfdfldkfldzjf;zdfkz;d"
Dim mymyWorkBook As Workbook
For Each mymyWorkBook In Workbooks
'code for creating checkbox based on the file name displayed by the workbook collection
Next mymyWorkBook
End Sub
I can't understand why that event is not getting triggered.Please help me to overcome this.Thanks in advance
Even though the name of the form is MyForm, you still need to use userform.
'~~> in your worksheet
Private Sub Button2_Click()
MyForm.Show
End Sub
'~~> In the userform code area
Private Sub UserForm_Initialize()
'~~> Your code here
End Sub
or
Private Sub UserForm_Activate()
End Sub
The best is to always select the event from the drop down, rather than typing it

Disconnect VBA UserForm from parent application

I'm using a UserForm spawned by Excel that modifies a PowerPoint presentation (it's a roundabout way to avoid needing a macro-enabled spreadsheet). The form works just fine, but every time I focus to it the Excel application takes focus (since Excel is the parent window).
Is there any way to stop this from happening? I'd like to prevent Excel from taking focus when the UserForm is used.
Would something like this work? This will hide/make invisible the parent Excel Application while the UserForm is displayed. Or at least get you started:
Example subroutine that "Shows" the userform:
Sub Test()
Dim ppt As Object
Dim xl As Object
Set ppt = GetObject(, "PowerPoint.Application")
Application.Visible = False
UserForm1.Show vbModeless
End Sub
Use this in the form's Terminate event:
Private Sub UserForm_Terminate()
'Ensures the Excel Application is visible after the form closes
Application.Visible = True
End Sub
You could add a button/etc on the form, if you want to allow the user to unhide the Excel Application
Private Sub CommandButton1_Click()
'Displays the Excel Application:
Application.Visible = True
End Sub