1

I want to close a workbook and suppress the save changes prompt when I double-click a particular cell. Of the three methods that I have tried, the first does not work, and the others crash Excel 2010; they only work in 2016.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Address(False, False) = "T1" Then
'        ThisWorkbook.Saved = True                  ' Method 1
'        ActiveWorkbook.Close savechanges:=False    ' Method 2
        Application.DisplayAlerts = False           ' Method 3
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
    End If
End Sub

Solution

Thanks to Arul for referring me to this answer:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Address(False, False) = "T1" Then
        Cancel = True
        Application.OnTime Now, "Close_Excel"
    End If
End Sub

with this in a standard module per this answer:

Sub Close_Excel()
    Application.Quit
    ThisWorkbook.Close savechanges:=False
End Sub
user2319146
  • 371
  • 1
  • 11

1 Answers1

0

I have tried this code in my workbook, but my workbook has also crashed along with this message box:

Excel Crash Message

I am currently using the Excel 2019 64-bit.

It seems a bug!

But I have two workarounds for you:

Workaround #1

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  If Target.Address(False, False) = "T1" Then
    Application.DisplayAlerts = False
    Application.Quit
  End If
End Sub

But this code will close not only the current workbook, but the whole application.


Workaround #2

The credit to this answer.

Use this code in the Worksheet_BeforeDoubleClick event:

If Target.Address(False, False) = "T1" Then
     Call Module1.Close_Workbook1
End If

And use the following two procedures in a new module called (Module1):

Sub Close_Workbook1()
        Application.OnTime Now, "Close_Workbook2"
End Sub

Private Sub Close_Workbook2()
        ThisWorkbook.Close SaveChanges:=False
End Sub
  • 1
    Thank you: #1 does what I want; #2 leaves the application open after closing the workbook. – user2319146 Dec 14 '18 at 08:26
  • The benefit of the #2 workaround comes when you have other open workbooks along with current workbook, and you want to close only the current one and keep the others open. – Haytham Amairah Dec 14 '18 at 09:04