Salut chers tous ,
J'aimerais ajouter sur mon Userform un bouton "ENREGISTRER" Qui va enregistrer et quitter excel si mon classeur actif est le seul ouvert sinon enregistrer et fermer simplement . Merci
L'instructionWorkbooks.Countpermet de savoir combien de classeurs sont ouverts. Toutefois, tu peux, si tu utilises le fichier des macro personnellesPERSONAL.XLSBavoir 2 classeurs ouverts. Sauf que le classeur des macros personnelles est caché.
Donc il faut que tu vérifies avant si tu utilises un classeur des macro personnelles avec le code :
VB:
Sub Macro1()
For I = 1 To Workbooks.Count
MsgBox Workbooks(I).Name
Next I
End Sub
Selon le cas le code sera
• avec le fichier des macros personnelles :
VB:
Sub Macro1()
If Workbooks.Count = 2 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
End Sub
• sans le fichier des macros personnelles :
VB:
Sub Macro1()
If Workbooks.Count = 1 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
End Sub
L'instructionWorkbooks.Countpermet de savoir combien de classeurs sont ouverts. Toutefois, tu peux, si tu utilises le fichier des macro personnellesPERSONAL.XLSBavoir 2 classeurs ouverts. Sauf que le classeur des macros personnelles est caché.
Donc il faut que tu vérifies avant si tu utilises un classeur des macro personnelles avec le code :
VB:
Sub Macro1()
For I = 1 To Workbooks.Count
MsgBox Workbooks(I).Name
Next I
End Sub
Selon le cas le code sera
• avec le fichier des macros personnelles :
VB:
Sub Macro1()
If Workbooks.Count = 2 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
End Sub
• sans le fichier des macros personnelles :
VB:
Sub Macro1()
If Workbooks.Count = 1 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
End Sub
Sub Macro1()
Dim CL As Workbook
Dim TEST As Boolean
For Each CL In Workbooks
If UCase(Right(CL.Name)) = "XLSB" Then TEST = True
End If
Select Case TEST
Case True
If Workbooks.Count = 2 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
Case False
If Workbooks.Count = 1 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
End Select
End Sub
Remarque : il faut pas nous le fâcher notre Job ! Après il devient colère et c'est tout le site qui en pâtit... Empathie...
Sub Macro1()
Dim CL As Workbook
Dim TEST As Boolean
For Each CL In Workbooks
If UCase(Right(CL.Name)) = "XLSB" Then TEST = True
End If
Select Case TEST
Case True
If Workbooks.Count = 2 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
Case False
If Workbooks.Count = 1 Then
ThisWorkbook.Close True
Application.Quit
Else
ThisWorkbook.Close True
End If
End Select
End Sub
Remarque : il faut pas nous le fâcher notre Job ! Après il devient colère et c'est tout le site qui en pâtit... Empathie...
Sub quitter()
Dim i&, Nombre&
For i = 1 To Workbooks.Count
If UCase(Workbooks(i).Name) = "PERSONAL.XLSB" Then Nombre = Workbooks.Count - 1: Exit For
Next
With ThisWorkbook
If Nombre = 1 Then
.Save
Application.Quit
Else
.Close True
End If
End With
End Sub
Cette macro prend en compte tous les fichiers ouverts dont les fenêtres sont masquées :
Code:
Sub Fermer()
Dim wb As Workbook, win As Window
If Not ThisWorkbook.Saved Then ThisWorkbook.Save
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then
If wb.Saved Then
For Each win In wb.Windows
If win.Visible Then ThisWorkbook.Close 'ferme le fichier
Next win
Else
ThisWorkbook.Close 'ferme le fichier
End If
End If
Next wb
Application.Quit 'ferme Excel
End Sub
Comme je m'étais trompé de discussion (Merci Job75 ), faut dire que les 2 avatars se ressemblaient, je remet le code.
Suivant ce que je veux faire j'utilise un de ces codes:
VB:
Sub Ferme_Et_Sauve_Juste_ce_Fichier()
'Ferme excel si seul ce fichier est ouvert
Dim w As Window
For Each w In Windows
If w.Caption = ThisWorkbook.Name Then GoTo suite
'Workbooks(w.Caption).Close True
suite:
Next w
ThisWorkbook.Save
If Workbooks.Count = 1 Then Application.EnableEvents = False: Application.Quit
ThisWorkbook.Close
End Sub
Sub Ferme_Tout_ET_Sauve()
Dim w As Window
For Each w In Windows
If w.Caption = ThisWorkbook.Name Then GoTo suite
Workbooks(w.Caption).Close True
suite:
Next w
ThisWorkbook.Save
Application.EnableEvents = False
Application.Quit
End Sub
Sub Ferme_Tout_Rend_Visible_Et_Sauve()
Dim w As Window
For Each w In Windows
If w.Visible = False Then w.Visible = True
If w.Caption = ThisWorkbook.Name Then GoTo suite
Workbooks(w.Caption).Close True
suite:
Next w
ThisWorkbook.Save
Application.EnableEvents = False
Application.Quit
End Sub
Sub Ferme_Tout_Sans_Sauver()
Dim w As Window
For Each w In Windows
If w.Caption = ThisWorkbook.Name Then GoTo suite
Workbooks(w.Caption).Close False
suite:
Next w
ThisWorkbook.Close 0
End Sub
Comme je m'étais trompé de discussion (Merci Job75 ), faut dire que les 2 avatars se ressemblaient, je remet le code.
Suivant ce que je veux faire j'utilise un de ces codes:
VB:
Sub Ferme_Et_Sauve_Juste_ce_Fichier()
'Ferme excel si seul ce fichier est ouvert
Dim w As Window
For Each w In Windows
If w.Caption = ThisWorkbook.Name Then GoTo suite
'Workbooks(w.Caption).Close True
suite:
Next w
ThisWorkbook.Save
If Workbooks.Count = 1 Then Application.EnableEvents = False: Application.Quit
ThisWorkbook.Close
End Sub
Sub Ferme_Tout_ET_Sauve()
Dim w As Window
For Each w In Windows
If w.Caption = ThisWorkbook.Name Then GoTo suite
Workbooks(w.Caption).Close True
suite:
Next w
ThisWorkbook.Save
Application.EnableEvents = False
Application.Quit
End Sub
Sub Ferme_Tout_Rend_Visible_Et_Sauve()
Dim w As Window
For Each w In Windows
If w.Visible = False Then w.Visible = True
If w.Caption = ThisWorkbook.Name Then GoTo suite
Workbooks(w.Caption).Close True
suite:
Next w
ThisWorkbook.Save
Application.EnableEvents = False
Application.Quit
End Sub
Sub Ferme_Tout_Sans_Sauver()
Dim w As Window
For Each w In Windows
If w.Caption = ThisWorkbook.Name Then GoTo suite
Workbooks(w.Caption).Close False
suite:
Next w
ThisWorkbook.Close 0
End Sub