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