PB thisWorkBook.Close

Bonjour à tous,

Je vous sollicite une nouvelle fois pour un problème de VBA

C'est un petit programme qui permet des créer des titres bien précis.

Le fichier excel est masqué pour qu'il n'y est que le USF qui apparait et à la fermeture le classeur se ferme mais voilà à la fermeture de l'USF il arrive qu'il ferme un autre excel si on en a un 2ème d'ouvert et après impossible de rouvrir le fichier qu'il a fermé par mégarde car en lecture seul et bloqué par l'utilisateur en question.

J'aimerai contourné ce dysfonctionnement si cela est possible je vous joint le fichier sans le code de masquage d'excel ni sa fermeture pour pas que vous soyez embéter pour accéder au code.

Voici le code entier que j'utilise sur la version finale avec masquage et fermeture:

Private Sub ComboBox1_Change()

'Changement en fonction de l'énergie'

If ComboBox1.Value = "Gaz" Then

TextBox1.Visible = True

Label3.Visible = True

TextBox2.Visible = False

Label4.Visible = False

ComboBox2.Visible = True

Label7.Visible = True

ComboBox3.Visible = False

Label8.Visible = False

TextBox3.Visible = True

Label5.Visible = True

ComboBox4.Visible = False

Label6.Visible = False

Label12.Visible = False

Else

If ComboBox1.Value = "Elec" Then

TextBox1.Visible = False

Label3.Visible = False

TextBox2.Visible = True

Label4.Visible = True

ComboBox2.Visible = False

Label7.Visible = False

ComboBox3.Visible = True

Label8.Visible = True

TextBox3.Visible = False

Label5.Visible = False

ComboBox4.Visible = True

Label6.Visible = True

Label12.Visible = False

Else

If ComboBox1.Value = "Gaz/Elec" Then

TextBox1.Visible = True

Label3.Visible = True

TextBox2.Visible = True

Label4.Visible = True

ComboBox2.Visible = False

Label7.Visible = False

ComboBox3.Visible = True

Label8.Visible = False

TextBox3.Visible = True

Label5.Visible = True

ComboBox4.Visible = True

Label6.Visible = True

Label12.Visible = True

End If

End If

End If

End Sub

Private Sub CommandButton1_Click()

'résultat en fonction des critères'

If OptionButton1 = True And ComboBox1.Value = "Gaz" Then

TextBox6 = ("Urgent/") & ComboBox1.Value & ("/") & TextBox1.Value & ("PCE") & ("/") & ComboBox2.Value & ("/") & TextBox3.Value & ("KWH") & ("/") & ("POUR LE ") & TextBox5.Value

End If

If OptionButton1 = False And ComboBox1.Value = "Gaz" Then

TextBox6 = ComboBox1.Value & ("/") & TextBox1.Value & ("PCE") & ("/") & ComboBox2.Value & ("/") & TextBox3.Value & ("KWH") & ("/") & ("POUR LE ") & TextBox5.Value

End If

If OptionButton1 = True And ComboBox1.Value = "Elec" Then

TextBox6 = ("Urgent/") & ComboBox1.Value & ("/") & TextBox2.Value & ("PDL") & ("/") & ComboBox3.Value & ("/") & ComboBox4.Value & ("/") & ("POUR LE ") & TextBox5.Value

End If

If OptionButton1 = False And ComboBox1.Value = "Elec" Then

TextBox6 = ComboBox1.Value & ("/") & TextBox2.Value & ("PDL") & ("/") & ComboBox3.Value & ("/") & ComboBox4.Value & ("/") & ("POUR LE ") & TextBox5.Value

End If

If OptionButton1 = True And ComboBox1.Value = "Gaz/Elec" Then

TextBox6 = ("Urgent/") & ComboBox1.Value & ("/") & TextBox1.Value & ("PCE") & ("/") & TextBox2.Value & ("PDL") & ("/") & ComboBox3.Value & ("/") & TextBox3.Value & ("KWH") & ("/") & ComboBox4.Value & ("/") & ("POUR LE ") & TextBox5.Value

End If

If OptionButton1 = False And ComboBox1.Value = "Gaz/Elec" Then

TextBox6 = ComboBox1.Value & ("/") & TextBox1.Value & ("PCE") & ("/") & TextBox2.Value & ("PDL") & ("/") & ComboBox3.Value & ("/") & TextBox3.Value & ("KWH") & ("/") & ComboBox4.Value & ("/") & ("POUR LE ") & TextBox5.Value

End If

End Sub

Private Sub CommandButton2_Click()

Dim Données As Object

Dim presse_papier As Variant

'récupération champs du formulaire

presse_papier = TextBox6.Value

'alimentation presse-papier

Set Données = New DataObject

Données.SetText (presse_papier)

Données.PutInClipboard

MsgBox ("Libellé copié !")

ThisWorkbook.Close True

End Sub

Private Sub OptionButton1_Click()

End Sub

Private Sub TextBox6_Change()

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

'sécurité et fermeture du classeur'

If CloseMode = 0 Then

If MsgBox("voulez-vous vraiment quitter ?", vbYesNo, "Attention") = vbYes Then

ThisWorkbook.Close True

Else

'empêcher la fermeture

Cancel = True

End If

End If

End Sub

D'avance merci pour votre temps et vos réponses.

Vinno

Bonjour,

Essaye comme ça indiquant en dur le nom du classeur à fermer à la place de ThisWorkbook.Close :

Workbooks("nom_classeur.xlsm").Close savechanges:=False

PS : Inutile de copier le code entier dans le message, qui ne facilite pas la lecture, alors que le fichier en cotient.

Bonjour Raja,

Merci pour ta réponse, cela semble fonctionner

Vinno

Rechercher des sujets similaires à "thisworkbook close"