I have this code that disable the close [x] function of excel. after completed enter ID, I need to enable again the button.
I have this part that disable the [x],
Public FlagToClose As Boolean
Sub Enter_1()
FlagToClose = False
'code
'.....................
FlagToClose = True
End Sub
for the workbook module I have this,
Private Sub Workbook_Open()
FlagToClose = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not FlagToClose Then Cancel = True
End Sub
then how to enable again the close[x] function?
To re-enable closing, run this short macro before closing:
Sub ev_off()
Application.EnableEvents = False
End Sub
Related
My understanding is that initialization code runs when I invoke "New UserForm2".
Below is the code that invokes a New userform and the code associated with the userform.
I stepped through with the debugger and watched it execute the initialization code, but it has no affect on the form. The combo box is still blank.
If TabID = "5 Client List" Then
With New UserForm2
.Show
If Not .IsCancelled Then
If InStr(TarSheet, "5.1") Then
Call General_Transfer(TabID, TarSheet, 28, "Yes")
End If
TarSheet = ""
End If
End With
Exit Sub
End If
' UserForm2 Code
Option Explicit
'#Folder("UI")
Private cancelled As Boolean
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelled
End Property
Private Sub CommandButton1_Click()
TarSheet = Me.ComboBox1.Value
Hide
End Sub
Private Sub CommandButton2_Click()
TarSheet = ""
OnCancel
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
OnCancel
End If
End Sub
Private Sub OnCancel()
cancelled = True
Hide
End Sub
Private Sub UserForm_Initialize()
UserForm2.ComboBox1.AddItem "5.1 Client List LTS"
End Sub
So far I have used the below VBA in order to continue with a procedure if the user clicked ok in the MsgBox:
Sub Button_Message_Box()
Answer = MsgBox("Do you want to continue the procedure?", vbOK)
If Answer = vbOK Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
Now I want to achieve the exact same result using CommandButton1 in UserForm1.
Therefore I tried to go with this:
(1) VBA in UserForm1:
Private Sub CommandButton1_Click()
Unload Me
End Sub
(2) VBA in Modul1:
Sub Button_Procedure()
Call UserForm1.Show(vbModeless)
If CommandButton1 = True Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
The VBA goes through but it does not enter the value 1 into Cell A1.
What do I need to modify to achieve the desired result?
I strongly suggest to follow the steps in this article: Rubberduck: UserForm1.Show
Nevertheless, a simple and dirty implementation could be as follows:
The form's code behind:
Add an event to raise when the OK-Cancel button has been pressed passing a boolean value indicating either to proceed or not:
Public Event OnClose(ByVal bool As Boolean)
Private Sub CmdOK_Click()
RaiseEvent OnClose(True)
End Sub
Private Sub CmdCancel_Click()
RaiseEvent OnClose(False)
End Sub
A simple wrapper class:
Here, we just instantiate the form and listen to the OnClose() event.
Option Explicit
Private WithEvents objForm As UserForm1
Private m_flag As Boolean
Public Function Show() As Boolean
Set objForm = New UserForm1
objForm.Show ' No vbModeless here, we want to halt code execution
Show = m_flag
End Function
Private Sub CloseForm()
Unload objForm
Set objForm = Nothing
End Sub
Private Sub objForm_OnClose(ByVal bool As Boolean)
m_flag = bool
CloseForm
End Sub
Calling the wrapper class:
Sub Something()
Dim bool As Boolean
With New FormWrapper
bool = .Show
End With
MsgBox "Should I proceed? " & bool
End Sub
With reference to this question I used a Boolean variable:
(1) Code in UserForm1:
Private continue_procedure As Boolean
Private Sub CommandButton1_Click()
continue_procedure = True
Unload Me
End Sub
Function check_procedure() As Boolean
UserForm1.Show
check_procedure = continue_procedure
End Function
(2) Code in Modul1:
Sub Button_Procedure()
If UserForm1.check_procedure() = True Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
This clearly shows that value of cancel is true, and still my workbook closes after this! Why?
I want to cancel closing of the excel file if user input is incorrect.
I debug the code and found out that the value of Cancel was initially False, and it becomes True. But once the Sub ends, file is still closed.
(variable error is defined at workbook declaration space, so accessible within module.)
Private Sub Workbook_BeforeClose(Cancel As Boolean)
error = 0
check_for_error
If error = 1 Then
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End Sub
I want to cancel closing of file if error = 1.
Update:: Thanks everyone for the responses, but nothing seems to work for me! Following is how my current code looks.
Option Explicit
Dim errYes As Byte
Private Sub Workbook_BeforeClose(Cancel As Boolean)
check_freeze_panel
If errYes = 1 Then
Cancel = True
Exit Sub
End If
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
errYes is returning 1, Cancel = True is being executed, and still my workbook closes.
I even tried commenting all the code, and just putting Cancel = True as suggested and guess what, it still closes!
Change the name of error this is a reserved word for an already existing function (see Error function) and must not be used as a variable name. And activate Option Explicit and declare all your variables.
Here is an example how you would do it correctly:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ErrorsFound As Boolean
ErrorsFound = check_for_error
If ErrorsFound Then
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End Sub
Function check_for_error() As Boolean
'check and return
check_for_error = True 'errors found
End Function
Or even shorter:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = check_for_error
If Not Cancel Then
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End If
End Sub
Function check_for_error() As Boolean
'check and return
check_for_error = True 'errors found
End Function
Try this. Set Error equal to your function call and make sure it returns the right data type.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
error = check_for_error
If error = 1 Then
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End Sub
Initially Cancel is equal False, means File Close will succeed.
For me the below code works fine. When I set error = 1 then closing is cancelled whether you use "Exit Sub" or not. When I set error <> 1 then cls
When I test your Code:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim error As Integer
error = 1
If error = 1 Then
Cancel = True
'Exit Sub
End If
End Sub
Just test this:
Write False and workbook should Close, and True should "stop" the closing.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = False
End Sub
Right now my process is working correctly, with the help of this community, however, I need this Worksheet_Calculate to NOT execute the Macro (MacroRuns for example) when the workbook opens, but I still need it to function the same way it is currently, after the workbook is opened.
Thank you so much for your help in advance!
The Code I Am Using:
in ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
TargetStart
End Sub
in the target sheet's code window
Option Explicit
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
TargetCalc Me
Application.EnableEvents = True
End Sub
in Module 1
Option Explicit
Public TargetValue As Variant
Private Const cTarget As String = "C3"
Sub TargetCalc(ws as Worksheet)
If ws.Range(cTarget) <> TargetValue Then
'this is where I would like the code to say something like, "if workbook just opened, exit -- otherwise continue. If this is even possible.
Call MacroRuns
TargetValue = ws.Range(cTarget).Value
End If
End Sub
Sub TargetStart()
TargetValue = Sheet1.Range(cTarget).Value
End Sub
Sub MacroRuns()
Call UpdateMsgBox
End Sub
I think that this can solve your problem:
In ThisWorkbook:
Private Sub Workbook_Open()
Worksheets("NameHere").Range("A1") = True
End Sub
In the target sheet's code window:
Private Sub Worksheet_Calculate()
If Worksheets("NameHere").Range("A1") Then MacroRuns
Worksheets("NameHere").Range("A1") = False
End Sub
Please try this arrangement.
Public StartUp As Boolean
Private Sub Workbook_Open()
StartUp = True
TargetStart
End Sub
Sub TargetCalc(ws As Worksheet)
If ws.Range(cTarget) <> TargetValue Then
If Not StartUp Then MacroRuns
StartUp = False
TargetValue = ws.Range(cTarget).Value
End If
End Sub
or, perhaps you prefer it to be like this.
Sub TargetCalc(ws As Worksheet)
If ws.Range(cTarget) <> TargetValue Then
If Not StartUp Then
MacroRuns
TargetValue = ws.Range(cTarget).Value
End If
StartUp = False
End If
End Sub
I have an .xlsm file which i want to run the macro automatically when i open it. The current file is saving the file as .xls in a different location with a different name before saving and before closing. However before closing is giving me error and so is the autorun of macro. Here is my code.
Private Sub Workbook_Open()
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Application.DisplayAlerts = False ' so you can overwrite without warning
ActiveWorkbook.SaveCopyAs "C:\Users\name\Desktop\testing.xls"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ScreenUpdating = False
Application.DisplayAlerts = False ' so you can overwrite without warning
ActiveWorkbook.SaveCopyAs "C:\Users\name\Desktop\testing.xls"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
ActiveWorkbook.save
End Sub
End Sub
For starters, you can't have events inside another event like that. You have a Workbook_BeforeSave and Workbook_BeforeClose inside your Workbook_Open... That won't work, and in any case I'm not sure what it would even mean!
Private Sub Workbook_Open()
' This won't work:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'...
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'...
End Sub
End Sub
You need to set up your events separately, like this:
Private Sub Workbook_Open()
'Stuff to do when file opens...
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Stuff to do before user saves the file...
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Stuff to do before the user closes the file...
End Sub
Also, instead of ActiveWorkbook, consider using Me (or ThisWorkbook) to avoid any ambiguity.