Case vbNo not working

kmendez

New Member
Joined
Oct 24, 2011
Messages
10
Hello there, can you please tell me whats wrong with case VbNo?
its suposed to close the workboo when user select "No"


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True

Application.DisplayAlerts = False

Dim exit1 As Integer

exit1 = MsgBox(" Would you like to save changes?", vbYesNoCancel, "Exit?")
Select Case exit1

Case vbYes
Call SaveAsNameInCells
Exit Sub


Case vbCancel
Cancel = True
Application.EnableEvents = True
Exit Sub

Case vbNo
ThisWorkbook.Close savechanges:=False

End Select
End Sub


-Thank's in advance;)
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This is inside the BeforeClose event, something is already trying to Close the workbook.
Telling it to Close is causing a cascade of BeforeClose events.

try this
Code:
Case vbNo
    ThisWorkbook.Saved = True
End Select
 
Upvote 0
Thank you mikerickson, still not working though
here is the entire VBA code maybe the mistake is in other part of the code

Code:
Private Sub Workbook_Open()

'Invoice number sequence increment
 [E5] = [E5] + 1

'Invoice Tax code sequence increment
 [D9] = [D9] + 1

'Auto save
 Me.Save

End Sub

Sub SaveAsNameInCells()

Dim sFileName As String
'Location to save the file
 Const sPath As String = "C:\Documents and Settings\carolina\Mis documentos\Cuentas\In Models Management\Agencia\Administrativo\Facturacion\Fiscal\"
 On Error Resume Next

'Name new file as invoice number + client name
 With ActiveWorkbook
 Sheets(1).Name = sFileName
 sFileName = Range("E5").Value & Range("D12").Value
 End With

 Application.EnableEvents = False
 If Len(Dir(sPath)) = 0 Then MkDir sPath
 ChDir sPath
    
'Show the open dialog and pass the selected file name to the String variable "sFileName"
 sFileName = Application.GetSaveAsFilename(sFileName, "Excel Files (*.xls),*.xls")
    
'If user cancels
 If sFileName = "False" Then Exit Sub
 
'Save Worbook
 ThisWorkbook.SaveAs sFileName
 Application.EnableEvents = True
 On Error GoTo 0
 
'Delete all code in new file
 Call DeleteAllVBACode
 Exit Sub
 
 'If client's name cell is empty
exit_handler:
        MsgBox "In Models: digite el nombre del cliente", vbCritical, "Informacion necesaria"
        Application.EnableEvents = True
       
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
Application.DisplayAlerts = False

'Custom message box
 Dim exit1 As Integer
 exit1 = MsgBox(" In Models deseas salvar esta factura?", vbYesNoCancel, "Exit?")
 Select Case exit1

'If user hits "Yes"
 Case vbYes
 Call SaveAsNameInCells
 Exit Sub

'If user hits "Cancel"
 Case vbCancel
 Cancel = True
 Application.EnableEvents = True
 Exit Sub
 
 'If user hits "No"
 Case vbNo
 ThisWorkbook.Saved = True
 End Select

End Sub


Sub DeleteAllVBACode()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        
        Set VBProj = ActiveWorkbook.VBProject
        
        For Each VBComp In VBProj.VBComponents
        If VBComp.Type = vbext_ct_Document Then
        Set CodeMod = VBComp.CodeModule
        With CodeMod
        .DeleteLines 1, .CountOfLines
        End With
        Else
        VBProj.VBComponents.Remove VBComp
        End If
        Next VBComp
End Sub

Thank's in advance.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,794
Members
449,468
Latest member
AGreen17

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