VBA - CloseMode to not allow use of "X" close button

AAF4348

New Member
Joined
Jun 30, 2011
Messages
4
Hi all. I am trying to force users of a spreadsheet to use a CLOSE button I have added to the workbook. When the button is clicked, the following code is used:


Sub Clse_Sve()

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.CutCopyMode = False

Sheets("UserTRX").Visible = True
Sheets("UserTRX").Select
Set mycell = Cells(65000, 1).End(xlUp).Offset(1, 0)
mycell.Select
mycell.Formula = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set mycell2 = ActiveCell.Offset(0, 1)
Set mycell3 = ActiveCell.Offset(0, 2)
Set mycell4 = ActiveCell.Offset(0, 3)
mycell2.Select
ActiveCell.Value = UserNameWindows_c
mycell3.Select
ActiveCell.Value = ComputerNameWindows_c
mycell4.Select
ActiveCell.Value = "File Closed"
Range("A1").Select
Sheets("Costing").Select
Sheets("UserTRX").Visible = False

Range("A11").Select

ActiveWorkbook.Save
ThisWorkbook.Close
End Sub
--------------------------------------------------------
Function UserNameWindows_c() As String
UserNameWindows_c = Environ("username")
End Function
--------------------------------------------------------
Function ComputerNameWindows_c() As String
ComputerNameWindows_c = Environ("computername")
End Function


I have the following Private sub loaded as well to keep the "X" close button from being used:


Private Sub Workbook_BeforeClose(Cancel As Boolean)
CloseMode As Integer
If CloseMode = 0 Then
MsgBox "Please use the CLOSE button."
Cancel = True
Else: End If
End Sub

When I am testing the code using F8 to run through each line, the Cls_Sve sub works fine and takes me to the private sub. This will then give me the MsgBox and cancel the closing of the workbook.

Any ideas? Thanks in advance!!!!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I figured it out!! I simply put the code I wanted to have done in the Private Sub BeforeClose procedure:


Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.CutCopyMode = False

Sheets("UserTRX").Visible = True
Sheets("UserTRX").Select
Set mycell = Cells(65000, 1).End(xlUp).Offset(1, 0)
mycell.Select
mycell.Formula = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set mycell2 = ActiveCell.Offset(0, 1)
Set mycell3 = ActiveCell.Offset(0, 2)
Set mycell4 = ActiveCell.Offset(0, 3)
mycell2.Select
ActiveCell.Value = UserNameWindows_c
mycell3.Select
ActiveCell.Value = ComputerNameWindows_c
mycell4.Select
ActiveCell.Value = "File Closed"
Range("A1").Select
Sheets("Costing").Select
Sheets("UserTRX").Visible = False

Range("A11").Select

ActiveWorkbook.Save

End Sub
--------------------------------------------------
Function UserNameWindows_c() As String
UserNameWindows_c = Environ("username")
End Function
-----------------------------------------------------
Function ComputerNameWindows_c() As String
ComputerNameWindows_c = Environ("computername")
End Function

Works like a charm!:):cool:
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top