Cannot reach vba code after Userform X-Button modification - excel

I inserted
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
unload me
thisworkbook.close
End Sub
into my userform1. I actually have 3 buttons on that form but after inserting the code above the workbook closes after clicking on any of these buttons. Any idea how i can reach my vba-editor now?

Ok, as far as I understand you created macro in userform which always closes this workbook - pretty unlucky :)
It is easy way out of it. Turn on VB Editor in any Excel file and run this code:
Application.EnableEvents = False
This will turn off of executing any events, so it will not close your workbook anymore. After you are done editing your code you can easily turn it on by running this code:
Application.EnableEvents = True

Related

Why macros are disabled each time I re-start my Excel workbook, even after enabling content...?

Here the context : I'm working on a local workbook (c:) that directly opens a UserForm within the Workbook_Open() Sub. Then, when UserForm is closed, Excel is also automatically closed. Nothing special there.
The problem : Even with a correct setup in my Trust Center Settings (Disable all macros WITH notification), the warning security message pops up again each time I reload that workbook.
Here my code :
Private Sub Workbook_Open()
' modal display of my form
myUserForm.Show vbModal
' upon return from my form, save and close that workbook
Me.Close savechanges:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' also quit Excel app if no more workbook is opened
If Application.Workbooks.Count = 1 Then Application.Quit
End Sub
I'm pretty sure that's related to the fact that Excel is automatically closed by program, and not by user himself.
For example if I reduce the code to the strict minimum, like :
Private Sub Workbook_Open()
myUserForm.Show vbModal ' modal display of my form
End Sub
In that situation, so without any Close or Quit, I just fall into my main worksheet when the UserForm is closed, then I just manually close Excel, with or without saving, and that's it, warning popup will not appear next time I'll open that workbook.
I don't know if there is a solution without playing with Trust Locations or All macros enabled.
Any ideas ?
Thanks for your help

Restart Current UserForm

I want to restart the master userform for a project I'm working on excel.
I tried hide, unload other userforms, call UserForm_Initialize and show again but not worked. Some variables still in memory, close (X) button didn't work anymore etc.
I looked on internet and there is no solution . So i found that solution below.
I added another empty master userform and call my userform if user or my codes unload or close my userform, master userform automaticly show again. This works but i cant use vbModeless Because out of stack error
i wonder is there another (more proper) solution ?
my master userform codes:
Option Explicit
Private Sub UserForm_Click()
StartApp 'there is no escape :)
End Sub
Private Sub UserForm_Initialize()
StartApp
End Sub
Public Sub StartApp()
Me.Hide 'sometimes this form showing itself so i added everywhere :)
Splash.Show
Me.Hide
StartApp
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = True
StartApp
End Sub
Splash is my loader form. Load's other forms etc.
Edit 1: Solved vbModeless problem with this link
Still looking another (more proper) solution ? (Without master form)

Strange behavior when closing a workbook twice

Strange issue when closing a workbook twice from VBA (Workbook_BeforeClose)
Hi. This problem appears to me in an extremely simple workbook: Workbook_BeforeClose only.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close SaveChanges:=False
End Sub
If I open and close the workbook twice, the main Excel screen looks like this, and it's impossible to do something, I can only close it from the status bar:
If all you are trying to do is to not prompt the user to save changes, just play with the appropriate flags to 'trick' Excel that changes have already been saved.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub
This will allow the workbook to close, without prompting any changes to be saved, but this does not actually save them.
Notice the subtle difference between the words: Me.Saved and Me.Save.
Saved is a property that gets flipped to False when Excel detects changes were made as of the last save.
Save is a method - not a property as above - that actually will save the workbook.
Your workbook is already closing, which is what fired this event to begin with. No need to try to close it again within this event. Just tell Excel that no changes have been made since the last save, and it should close all on it's own - without the prompts.
It's possible you're re-triggering the event. Try something like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Static InProgress As Boolean
If InProgress Then Exit Sub
InProgress = True
ThisWorkbook.Close SaveChanges:=False
End Sub

How to combine listbox code in Excel VBA

I am trying to do an Excel project for work. It involves a lot of listboxes. I needed the listboxes to have a double click event where the single click selects and a double click deselects. After some research, I was able to find some code that would perform that function perfectly:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Application.EnableEvents = False
Me.ListBox1.ListIndex = -1
Application.EnableEvents = True
End Sub
However, there is so much code for all of the listboxes that I think it is making the file glitch. Excel has crashed several times. Each listbox just needs the double click event. Is there a way to combine all this into one general command? Please dumb down your responses, as I am not a programmer. I am in way over my head. Thanks!

Closing Excel UserForm on CommandButton click

I have a sheet that has a ListBox. The sheet code has a ListBox_DblClick sub that launches UserForm1. UserForm1.CommandButton1 launches UserForm2. The user may input data in UserForm2 then press UserForm2.CommandButton1 which launches a sub in a module (yet to be written) and closes UserForm2.
With the code below, both UserForm2 and UserForm1 close. I just want UserForm2 to close upon click of UserForm2.CommandButton1.
Any thoughts on what I'm doing wrong are appreciated.
UserForm2 code
Private Sub CommandButton1_Click()
Call UpdateBids(TextBox1.Object.Value, TextBox2.Object.Value, ComboBox1.Object.Value)
Unload Me
End Sub
Upon researching this again, I stumbled upon this post:
Why, after launching a userform from a userform, does unloading the 2nd userform close both?
In short it suggested closing Excel then adding DoEvents following UserForm2.Show. It worked. Now only my UserForm2 closes. I've not had the problem since.

Resources