Microsoft 365 fermer TextBox en fermant Excel

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour à toutes et à tous,
Je vous souhaite une belle journée :)

J'ai un souci de code qui beug et je n'arrive pas à trouver comment le modifier :
En feuille "Appels", pour faire des affichages de contenus de cellules, j'ouvre un TextBox1
Si oubli, je voudrais le fermer automatiquement en quittant Excel et j'ai fait ce code :
VB:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If TextBox1.Visible = True Then
    With TextBox1
        .Visible = False
    End With

    Sheets("Appels").Unprotect Password:=""
    With Sheets("Appels").Range("m1").Interior
        .ThemeColor = xlThemeColorDark1
    End With
    Sheets("Appels").Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
    End If
   
    ActiveWorkbook.Save
    ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
End Sub

la ligne qui bloque est : " If TextBox1.Visible = True Then"

Auriez-vous la solution ?
Si besoin, je ferais un petit fichier test.
Je vous remercie vivement,
Amicalement,
lionel,
 
Dernière édition:
Solution
re,

si la textbox est bien dans la feuille appels, cela devrait fonctionner comme cela.

Bien cordialement, @+
VB:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Sheets("Appels")
        If .TextBox1.Visible = True Then
            .TextBox1.Visible = False
            .Unprotect Password:=""
            .Range("m1").Interior.ThemeColor = xlThemeColorDark1
            .Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
    End With
    ActiveWorkbook.Save
End Sub
re,

si la textbox est bien dans la feuille appels, cela devrait fonctionner comme cela.

Bien cordialement, @+
VB:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Sheets("Appels")
        If .TextBox1.Visible = True Then
            .TextBox1.Visible = False
            .Unprotect Password:=""
            .Range("m1").Interior.ThemeColor = xlThemeColorDark1
            .Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
    End With
    ActiveWorkbook.Save
End Sub
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour Yeahou,

Merci pour ce code de bon matin :)
Grâce à toi, j'ai modifié comme suit :
VB:
With Sheets("Appels")
        If .TextBox1.Visible = True Then
        .TextBox1.Visible = False
        .Unprotect Password:=""
            With [m1].Interior
                .ThemeColor = xlThemeColorDark1
            End With
        .Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
    End With
Bonne journée,
lionel :)
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Re,
J'ai ton code en solution :)
Mais le code qui fonctionne est :
VB:
With Sheets("Appels")
Sheets("Appels")
        If .TextBox1.Visible = True Then
        .TextBox1.Visible = False
        .Unprotect Password:=""
            With [m1].Interior
                .ThemeColor = xlThemeColorDark1
            End With
        .Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
    End
:)
 

TooFatBoy

XLDnaute Barbatruc
Bonjour,

Mais le code qui fonctionne est :
VB:
With Sheets("Appels")
Sheets("Appels")
If .TextBox1.Visible = True Then
.TextBox1.Visible = False
.Unprotect Password:=""
With [m1].Interior
.ThemeColor = xlThemeColorDark1
End With
.Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End

1- À quoi sert le Sheets("Appels") tout seul ?

2- Le monsieur t'a dit de mettre un point devant [M1]. 😉

3- Pourquoi mettre un deuxième With ?

Et tant que j'y suis, j'ajouterais que ton indentation n'est pas totalement irréprochable... 😁
 

job75

XLDnaute Barbatruc
Bonjour Lionel, Yeahou, Marcel32,

Si l'on aime la simplicité :
VB:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Sheets("Appels")
        .Protect UserInterfaceOnly:=True
        .TextBox1.Visible = False
        .Range("m1").Interior.Color = xlColorDark1
    End With
    Save
End Sub
A+
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour Lionel, Yeahou, Marcel32,

Si l'on aime la simplicité :
VB:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Sheets("Appels")
        .Protect UserInterfaceOnly:=True
        .TextBox1.Visible = False
        .Range("m1").Interior.Color = xlColorDark1
    End With
    Save
End Sub
A+
Bonjour Gérard,

Merci d'avoir répondu à ce fil :)
Bizarrement "Usine à gaz" aime la simplicité lol
Et c'est nickel comme toujours.
Merci Gérard :)
lionel,
 

Discussions similaires

Statistiques des forums

Discussions
311 724
Messages
2 081 936
Membres
101 844
dernier inscrit
pktla