vba - why does userform come up twice after closing thisworkbook - vba

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.

Related

Limiting user interaction with the workbook - hide workbook / show only userform

Is it possible to get the same effect using ThisWorkbook.Application.Visible = False but only for one Workbook. I mean, I'd like to limiting user interaction only to UserForm, but I need have an access to anothers workbooks. At the moment this function cause hide workbook, but after open some another excel file - all object from userform are not available.
Private Sub Workbook_Open()
ThisWorkbook.Application.Visible = False
Starter.Show modeless
End Sub
Thanks for your support.
Please, create a form, let us say "Starter", having (at least) a button ("btExit"), copy the next code in its code module and show it. If the form in discussion already has some code in Initialize and Terminate events, please add the next code lines, too:
Option Explicit
Private Sub btExit_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
So, you can simple use workbook Open event in this way:
Private Sub Workbook_Open()
Starter.Show vbModeless
End Sub

How to implement a Cancel button on a userform?

I have a UserForm with a cancel button.
Sub DialogTest()
MyForm.Show
End Sub
Private Sub CancelButton_Click()
Unload Me
End
End Sub
I also tried MyForm.Hide, End by itself, cmdExit_Click.
The cancel button does not close the dialog nor does it cause the debugger to come up.
I was only able to replicate your issue when the Unload Me Sub was pasted in a Worksheet or Module. When the Sub is in the Userform, it works fine.
Here, the code is pasted in a module and does not close the Userform
Instead, from VBE, double click on your UserForm, then double click on your Cancel Button.
Then paste the code here

starting a userform without excel in the background

I have this vba code where it has a userform.
Now I would like to start the userform; where the people can't see the Worksheets.
I have added this in my code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True End Sub
Then in the "ThisWorkbook; I have added this:
Private Sub Workbook_Open()
Application.Visible = False ' only for final version
UserForm1.Show
'enter code here
End Sub
The thing is when I upload this .xlsm to a website, and when you open it; it does not start the userform right away. It somehow goes to an error. If you have an excel open.
Not sure how to protect the file, without having the endusers altering the file
Any ideas?
Maybe this helps:
In "ThisWorkbook" under "Open" add:
Private Sub Workbook_Open()
Application.Visible = False
UserForm2.Show vbModeless
End Sub
This will hide Excel and execute UserForm2.
BTW: To end your program add this code (e.g. to one of your Userform Buttons):
Private Sub CommandButton1_Click()
Application.Quit
End Sub
It will close Excel.

Workbook_BeforeSave with every active workbook ActiveWorkbook

As I very often have to problem, that the tick vanishes in the settings: Calculate before save. (I don't know the exact term as my office version is in German).
That's why I tried to use VBA to solve the problem. I used the following code in my Excel file:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
If Application.CalculateBeforeSave = False Then
If MsgBox("Caution! Should >calculatebeforesave< be activated?", vbYesNo) = vbYes Then
Application.CalculateBeforeSave = True
Else
End If
Else
End If
End Sub
I put this into "Thisworkbook". But I would like this code to be ran in every workbook I work with (at least all these which allow for macros).
My suggestion was to write ActiveWorkbook_BeforeSave... instead of Workbook_BeforeSave and then put the code in a module in the PERSONAL Macro file. But this doesn't work.
I think you need to use the Excel Applications events rather than workbook events to achieve this, such as in this example
In your PERSONAL workbook right click and insert a a Class Module (Class 1)
Add something similar to below to Class 1:
Public WithEvents appevent As Application
Private Sub appevent_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
'Add what you would like to happen before a workbook closes
End Sub
Next open ThisWorksheet and add code along these lines (I think the PERSONAL workbook opens automatically when Excel starts):
Dim myobject As New Class1
Private Sub Workbook_Open()
Set myobject = Application
End Sub

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