Masquer une feuille

Résolu
Bonjour,

Je rencontre un problème avec un UserForm, lorsque j'appuie sur le CommandButton "Valider".

Le code est censé m'afficher la feuille "VIERGE", la copier-coller dans un nouvel onglet.
=> à ce sujet : aucun problème
Tout mon code fonctionne parfaitement mais à la fin de celui-ci, je demande de masquer la feuille "VIERGE" et c'est là que ça coince !
Malgré plusieurs tentatives, quitte même à passer par l'enregistreur de macro, impossible de masquer cette feuille et vraiment je ne trouve pas pourquoi !

Voici une partie du code :

Private Sub CommandButton_Valider_Click()

code

'Affichage de la feuille "VIERGE"
Sheets("VIERGE").Visible = True

'On copie une fiche vierge
With Sheets("VIERGE").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
'Sélection du dernier onglet à droite
Sheets(Sheets.count).Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste
ActiveWindow.Zoom = 70
Range("C2").Select
Application.CutCopyMode = False

code

'Masquer la feuille "VIERGE"
Sheets("VIERGE").Select
ActiveWindow.SelectedSheets.Visible = False
    
'Sélection de la dernière feuille de droite
Sheets(Sheets.count).Select

End Sub


Configuration: Windows / Chrome 85.0.4183.121


--
Pensez au "merci" et au "résolu" quand le sujet l'est.

5 réponses

  1. Modérateur
    Bonjour,

    J'ai testé et la feuille se masque bien.

    Cela étant, à toutes fins utiles, il n'est pas nécessaire de la sélectionner pour la masquer :

     ActiveWorkbook.Sheets("VIERGE").Visible = False


    est suffisant.

    m@rina
    1
    1. Bonjour,

      J'avais pensé que partager mon fichier n'était pas utile, désolé.

      En plus de la réponse de m@rina que je salue, je ne vous avais pas demandé le fichier, simplement que le fait de ne connaître sa structure, je ne pouvais pas en dire plus, mais que le problème se situait entre les 2 parties de code citées et vous encourageait à chercher dans cette zone en faisant du pas à pas.

      J'ai fait tourner l'application, 2 fois,
      la première en remplissant le formulaire, après validation tout est OK.
      la deuxième en ne remplissant pas les zones, là aussi après validation tout est OK
      Le seul moment ou l'on ne cache pas la feuille, c'est si les 3 combobox en fond de formulaire ne sont pas remplies, mais dans ce cas, on ne ferme pas le formulaire tant que ce n'est pas totalement rempli.
          'Insertion du résultat des 3 calculs :
          <gras>If ComboBox_C1.Value = "" And ComboBox_C2.Value = "" And ComboBox_C3.Value = "" Then
              Exit Sub</gras>


      Donc tout est bon.

      Revoici le code légèrement modifié, qui respecte l'indentation et certains "End If" inutiles supprimés

      Private Sub CommandButton_Valider_Click()
          'Générer une fiche avec son n° d'ID
          Dim feuille As String
          'Nom de l'onglet
          feuille = Me.TextBox_N°ICP.Text & "-ST" & Me.ComboBox_ST.Value
          'On met en marche la gestion des erreurs
          On Error Resume Next
          'On active la feuille générée
          Sheets(feuille).Activate
          If Err > 1 Then ' la feuille n'existe pas on la crée
              Sheets.Add after:=Sheets(Sheets.count)
              ActiveSheet.Name = feuille
              'Elle existe
          Else
              'On la supprime
              Application.DisplayAlerts = False
              ActiveSheet.Delete
              Application.DisplayAlerts = True
              'On en crée une nouvelle qui porte le nom choisi
              Sheets.Add after:=Sheets(Sheets.count)
              ActiveSheet.Name = feuille
          End If
          ' on arrête la gestion des erreurs
          On Error GoTo 0
          
          'Affichage de la feuille "VIERGE"
          Sheets("VIERGE").Visible = True
          
          'On copie une fiche vierge
          With Sheets("VIERGE").Select
              Cells.Copy
              'Sélection du dernier onglet à droite
              Sheets(Sheets.count).Select
              Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
              ActiveSheet.Paste
              ActiveWindow.Zoom = 70
              
              'Suppression du quadrillage,de la barre de formule et de la barre d'en-tête :
              ActiveWindow.DisplayGridlines = False
              ActiveWindow.DisplayHeadings = False
              Application.DisplayFormulaBar = False
          End With
          
          'Insertion de l'auteur :
          If ComboBox_Noms_Prénoms.Value = "" Then
              MsgBox "Champ [Auteur] obligatoire", vbExclamation
              ComboBox_Noms_Prénoms.BackColor = RGB(255, 97, 97)
          Else
              Range("E4").Value = ComboBox_Noms_Prénoms.Value
          End If
          
          'Insertion de la date :
          If TextBox_Date.Value = "" Then
              MsgBox "Champ [Date] au format 00/00/0000 obligatoire", vbExclamation
              TextBox_Date.BackColor = RGB(255, 97, 97)
          Else
              Range("P4").Value = TextBox_Date.Value
          End If
          
          'Insertion du numéro d'ID Bottom Up
          If TextBox_N°ICP.Value = "" Then
              MsgBox "Champ [ID B-U] obligatoire", vbExclamation
              TextBox_N°ICP.BackColor = RGB(255, 97, 97)
          Else
              Range("W4").Value = TextBox_N°ICP.Value
          End If
          
          'Insertion du n° de ST :
          If ComboBox_ST.Value = "" Then
              MsgBox "N° de la Small Team non renseigné", vbExclamation
              ComboBox_ST.BackColor = RGB(255, 97, 97)
          Else
              Range("Q6").Value = ComboBox_ST.Value
          End If
          
          'Insertion de la déscription de l'ICP :
          If TextBox_Déscription_ICP.Value = "" Then
              MsgBox "Champ [Déscription de l'ICP] obligatoire", vbExclamation
              TextBox_Déscription_ICP.BackColor = RGB(255, 97, 97)
          Else
              Range("B8").Value = TextBox_Déscription_ICP.Value
          End If
          
          'Insertion du résultat escompté :
          If OptionButton_Sécurité_Ergonomie = False And OptionButton_Coût = False And OptionButton_Qualité = False And OptionButton_Délai = False And OptionButton_Environnement = False And OptionButton_Propreté_Rangement = False Then
              MsgBox "Bouton du résultat escompté non coché", vbExclamation
              OptionButton_Sécurité_Ergonomie.BackColor = RGB(255, 97, 97)
              OptionButton_Coût.BackColor = RGB(255, 97, 97)
              OptionButton_Qualité.BackColor = RGB(255, 97, 97)
              OptionButton_Délai.BackColor = RGB(255, 97, 97)
              OptionButton_Environnement.BackColor = RGB(255, 97, 97)
              OptionButton_Propreté_Rangement.BackColor = RGB(255, 97, 97)
          Else
              If OptionButton_Sécurité_Ergonomie = True Then Range("B18").Value = "Sécurité - Ergonomie"
          End If
          If OptionButton_Coût = True Then Range("B18").Value = "Coût"
          If OptionButton_Qualité = True Then Range("B18").Value = "Qualité"
          If OptionButton_Délai = True Then Range("B18").Value = "Délai"
          If OptionButton_Environnement = True Then Range("B18").Value = "Environnement"
          If OptionButton_Propreté_Rangement = True Then Range("B18").Value = "Propreté - Rangement"
          
          'Insertion de la solution proposée
          If TextBox_Solution_Proposée <> "" Then Range("B21").Value = TextBox_Solution_Proposée.Value
          
          'Insertion de la validation
          If CheckBox_OUI = True Then
              Range("G30").Value = "X"
          ElseIf CheckBox_NON = True Then Range("L30").Value = "X"
          End If
          
          If CheckBox_OUI = True And CheckBox_NON = True Then MsgBox "L'accord est soit OUI soit NON", vbExclamation
          
          If CheckBox_OUI = False And CheckBox_NON = False Then
              MsgBox "Champ [Accord] non renseigné", vbExclamation
              CheckBox_OUI.BackColor = RGB(255, 97, 97)
              CheckBox_NON.BackColor = RGB(255, 97, 97)
          Else
              If CheckBox_NON = True And TextBox_NON_Pourquoi.Value = "" And CheckBox_OUI = False Then
                  MsgBox "Merci de remplir le pourquoi du refus de l'ICP", vbExclamation
                  TextBox_NON_Pourquoi.BackColor = RGB(255, 97, 97)
              End If
          End If
          
          If TextBox_NON_Pourquoi <> "" Then Range("E31").Value = TextBox_NON_Pourquoi.Value
          
          'Insertion de la date
          If ComboBox_day.Value = "" And ComboBox_month.Value = "" Then
              MsgBox "merci de remplir la date de prise en compte de l'accord ou du refus", vbExclamation
              ComboBox_day.BackColor = RGB(255, 97, 97)
              ComboBox_month.BackColor = RGB(255, 97, 97)
          Else
              Range("S29").Value = ComboBox_day.Value
              Range("V29").Value = ComboBox_month.Value
              Range("X29").Value = TextBox_year
          End If
          
          'Insertion du calcul 1
          If ComboBox_C1.Value <> "" And ComboBox_C2 = "" And ComboBox_C3 = "" Then
              MsgBox "Points des calculs 2 & 3 non renseignés", vbExclamation
              ComboBox_C2.BackColor = RGB(255, 97, 97)
              ComboBox_C3.BackColor = RGB(255, 97, 97)
          Else
              Range("H33").Value = ComboBox_C1.Value
          End If
          
          'insertion du calcul 2
          If ComboBox_C2.Value <> "" And ComboBox_C1.Value = "" And ComboBox_C3.Value = "" Then
              MsgBox "Points des calculs 1 & 3 non renseignés", vbExclamation
              ComboBox_C1.BackColor = RGB(255, 97, 97)
              ComboBox_C3.BackColor = RGB(255, 97, 97)
          Else
              Range("K33").Value = ComboBox_C2.Value
          End If
          
          'Insertion du calcul 3
          If ComboBox_C3.Value <> "" And ComboBox_C1.Value = "" And ComboBox_C2.Value = "" Then
              MsgBox "Points des calculs 1 & 2 non renseignés", vbExclamation
              ComboBox_C1.BackColor = RGB(255, 97, 97)
              ComboBox_C2.BackColor = RGB(255, 97, 97)
          Else
              Range("N33").Value = ComboBox_C3.Value
          End If
          
          'Insertion du résultat des 3 calculs :
          If ComboBox_C1.Value = "" And ComboBox_C2.Value = "" And ComboBox_C3.Value = "" Then
              Exit Sub
          Else
              If ComboBox_C1.Value <> "0" And ComboBox_C2.Value <> "0" And ComboBox_C3.Value <> "0" Then
                  Range("Q33").Value = ComboBox_C1.Value * ComboBox_C2.Value * ComboBox_C3.Value
              ElseIf ComboBox_C1.Value = "0" And ComboBox_C2.Value <> "0" And ComboBox_C3.Value <> "0" Then Range("Q33").Value = ComboBox_C2.Value * ComboBox_C3.Value
              End If
          End If
          
          'Masquer la feuille "VIERGE"
          Sheets("VIERGE").SelectedSheets.Visible = False
              
          'Sélection de la dernière feuille de droite
          Sheets(Sheets.count).Select
          Unload Me
          
          'UsF_ICP.Show
      End Sub


      Question: Quelles manoeuvres faites-vous pour que cela ne marche pas?

      Cdlt
      1
      1. Jamais je n'aurais pensé que si les ComboBox_C1/C2/C3 n'étaient pas remplies, que la feuille "VIERGE" ne se masque pas !

        Tu m'as éclairé Frenchie83 et j'ai donc remarqué cet
        Exit Sub
        si ces 3 ComboBox n'étaient pas remplies !

        J'ai donc modifié le code ainsi :
        'Insertion du résultat des 3 calculs :
        If ComboBox_C1.Value = "" And _
        ComboBox_C2.Value = "" And _
        ComboBox_C3.Value = "" Then
        ActiveWorkbook.Sheets("VIERGE").Visible = False
        'Exit Sub


        Un truc tout bête qui m'a fait faire perdre votre temps !

        Merci à toi aussi Marina
        0
    2. Bonjour

      Peut-être un "End With" manquant ?
      0
      1. Non, comme je l'ai dit, l'intégralité de mon code (associé au CommandButton "Valider") fonctionne.

        C'est juste que la feuille "VIERGE" ne se masque pas et j'ai du mal à saisir pourquoi !?
        0
    3. Bonjour,

      J'ai placé un "End With" manquant pour faire le test et la feuille "VIERGE" se masque bien.
      Où l'avez-vous placé ce fameux "End With"? C'est peut-être là votre problème.

      Autre chose, votre bouton s'appelle "Valider", alors pourquoi "Private Sub CommandButton_Valider_Click" au lieu de "Private Sub Valider_Click()"?

      Cdlt
      0
      1. Bonjour,
        je mets le code entier, rattaché au CommandButton (c'est moi qui l'ai renommé en CommandButton_Valider)

        Le End With est en ligne #50 :

        Private Sub CommandButton_Valider_Click()
        
        'Générer une fiche avec son n° d'ID
        Dim feuille As String
        'Nom de l'onglet
        feuille = Me.TextBox_N°ICP.Text & "-ST" & Me.ComboBox_ST.Value
        'On met en marche la gestion des erreurs
        On Error Resume Next
        'On active la feuille générée
        Sheets(feuille).Activate
        If Err > 1 Then ' la feuille n'existe pas on la crée
        Sheets.Add after:=Sheets(Sheets.count)
        ActiveSheet.Name = feuille
        'Elle existe
        Else
        'On la supprime
        Application.DisplayAlerts = False
        ActiveSheet.Delete
        Application.DisplayAlerts = True
        'On en crée une nouvelle qui porte le nom choisi
        Sheets.Add after:=Sheets(Sheets.count)
        ActiveSheet.Name = feuille
        End If
        ' on arrête la gestion des erreurs
        On Error GoTo 0
        
        'Affichage de la feuille "VIERGE"
        Sheets("VIERGE").Visible = True
        
        'On copie une fiche vierge
        With Sheets("VIERGE").Select
        Cells.Select
        Application.CutCopyMode = False
        Selection.Copy
        'Sélection du dernier onglet à droite
        Sheets(Sheets.count).Select
        Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        ActiveSheet.Paste
        ActiveWindow.Zoom = 70
        Range("C2").Select
        Application.CutCopyMode = False
        
        'Suppression du quadrillage,de la barre de formule et de la barre d'en-tête :
        ActiveWindow.DisplayGridlines = False
        ActiveWindow.DisplayHeadings = False
        Application.DisplayFormulaBar = False
        
        End With
        
        'Insertion de l'auteur :
        If ComboBox_Noms_Prénoms.Value = "" Then
        MsgBox "Champ [Auteur] obligatoire", vbExclamation
        ComboBox_Noms_Prénoms.BackColor = RGB(255, 97, 97)
        Else
        Range("E4").Value = ComboBox_Noms_Prénoms.Value
        End If
        
        'Insertion de la date :
        If TextBox_Date.Value = "" Then
        MsgBox "Champ [Date] au format 00/00/0000 obligatoire", vbExclamation
        TextBox_Date.BackColor = RGB(255, 97, 97)
        Else
        Range("P4").Value = TextBox_Date.Value
        End If
        
        'Insertion du numéro d'ID Bottom Up
        If TextBox_N°ICP.Value = "" Then
        MsgBox "Champ [ID B-U] obligatoire", vbExclamation
        TextBox_N°ICP.BackColor = RGB(255, 97, 97)
        Else
        Range("W4").Value = TextBox_N°ICP.Value
        End If
        
        'Insertion du n° de ST :
        If ComboBox_ST.Value = "" Then
        MsgBox "N° de la Small Team non renseigné", vbExclamation
        ComboBox_ST.BackColor = RGB(255, 97, 97)
        Else
        Range("Q6").Value = ComboBox_ST.Value
        End If
        
        'Insertion de la déscription de l'ICP :
        If TextBox_Déscription_ICP.Value = "" Then
        MsgBox "Champ [Déscription de l'ICP] obligatoire", vbExclamation
        TextBox_Déscription_ICP.BackColor = RGB(255, 97, 97)
        Else
        Range("B8").Value = TextBox_Déscription_ICP.Value
        End If
        
        'Insertion du résultat escompté :
        If OptionButton_Sécurité_Ergonomie = False And _
        OptionButton_Coût = False And _
        OptionButton_Qualité = False And _
        OptionButton_Délai = False And _
        OptionButton_Environnement = False And _
        OptionButton_Propreté_Rangement = False Then
        MsgBox "Bouton du résultat escompté non coché", vbExclamation
        OptionButton_Sécurité_Ergonomie.BackColor = RGB(255, 97, 97)
        OptionButton_Coût.BackColor = RGB(255, 97, 97)
        OptionButton_Qualité.BackColor = RGB(255, 97, 97)
        OptionButton_Délai.BackColor = RGB(255, 97, 97)
        OptionButton_Environnement.BackColor = RGB(255, 97, 97)
        OptionButton_Propreté_Rangement.BackColor = RGB(255, 97, 97)
        Else
        If OptionButton_Sécurité_Ergonomie = True Then
        Range("B18").Value = "Sécurité - Ergonomie"
        End If
        If OptionButton_Coût = True Then
        Range("B18").Value = "Coût"
        End If
        If OptionButton_Qualité = True Then
        Range("B18").Value = "Qualité"
        End If
        If OptionButton_Délai = True Then
        Range("B18").Value = "Délai"
        End If
        If OptionButton_Environnement = True Then
        Range("B18").Value = "Environnement"
        End If
        If OptionButton_Propreté_Rangement = True Then
        Range("B18").Value = "Propreté - Rangement"
        End If
        End If
        
        'Insertion de la solution proposée
        If TextBox_Solution_Proposée <> "" Then
        Range("B21").Value = TextBox_Solution_Proposée.Value
        End If
        
        'Insertion de la validation
        If CheckBox_OUI = True Then
        Range("G30").Value = "X"
        End If
        If CheckBox_NON = True Then
        Range("L30").Value = "X"
        End If
        
        If CheckBox_OUI = True And _
        CheckBox_NON = True Then
        MsgBox "L'accord est soit OUI soit NON", vbExclamation
        End If
        
        If CheckBox_OUI = False And _
        CheckBox_NON = False Then
        MsgBox "Champ [Accord] non renseigné", vbExclamation
        CheckBox_OUI.BackColor = RGB(255, 97, 97)
        CheckBox_NON.BackColor = RGB(255, 97, 97)
        Else
        If CheckBox_NON = True And _
        TextBox_NON_Pourquoi.Value = "" And _
        CheckBox_OUI = False Then
        MsgBox "Merci de remplir le pourquoi du refus de l'ICP", vbExclamation
        TextBox_NON_Pourquoi.BackColor = RGB(255, 97, 97)
        End If
        End If
        
        If TextBox_NON_Pourquoi <> "" Then
        Range("E31").Value = TextBox_NON_Pourquoi.Value
        End If
        
        'Insertion de la date
        If ComboBox_day.Value = "" And _
        ComboBox_month.Value = "" Then
        MsgBox "merci de remplir la date de prise en compte de l'accord ou du refus", vbExclamation
        ComboBox_day.BackColor = RGB(255, 97, 97)
        ComboBox_month.BackColor = RGB(255, 97, 97)
        Else
        Range("S29").Value = ComboBox_day.Value
        Range("V29").Value = ComboBox_month.Value
        Range("X29").Value = TextBox_year
        End If
        
        'Insertion du calcul 1
        If ComboBox_C1.Value <> "" And _
        ComboBox_C2 = "" And _
        ComboBox_C3 = "" Then
        MsgBox "Points des calculs 2 & 3 non renseignés", vbExclamation
        ComboBox_C2.BackColor = RGB(255, 97, 97)
        ComboBox_C3.BackColor = RGB(255, 97, 97)
        Else
        Range("H33").Value = ComboBox_C1.Value
        End If
        
        'insertion du calcul 2
        If ComboBox_C2.Value <> "" And _
        ComboBox_C1.Value = "" And _
        ComboBox_C3.Value = "" Then
        MsgBox "Points des calculs 1 & 3 non renseignés", vbExclamation
        ComboBox_C1.BackColor = RGB(255, 97, 97)
        ComboBox_C3.BackColor = RGB(255, 97, 97)
        Else
        Range("K33").Value = ComboBox_C2.Value
        End If
        
        'Insertion du calcul 3
        If ComboBox_C3.Value <> "" And _
        ComboBox_C1.Value = "" And _
        ComboBox_C2.Value = "" Then
        MsgBox "Points des calculs 1 & 2 non renseignés", vbExclamation
        ComboBox_C1.BackColor = RGB(255, 97, 97)
        ComboBox_C2.BackColor = RGB(255, 97, 97)
        Else
        Range("N33").Value = ComboBox_C3.Value
        End If
        
        'Insertion du résultat des 3 calculs :
        If ComboBox_C1.Value = "" And _
        ComboBox_C2.Value = "" And _
        ComboBox_C3.Value = "" Then
        Exit Sub
        Else
        If ComboBox_C1.Value <> "0" And _
        ComboBox_C2.Value <> "0" And _
        ComboBox_C3.Value <> "0" Then
        Range("Q33").Value = ComboBox_C1.Value * _
        ComboBox_C2.Value * _
        ComboBox_C3.Value
        Else
        If ComboBox_C1.Value = "0" And _
        ComboBox_C2.Value <> "0" And _
        ComboBox_C3.Value <> "0" Then
        Range("Q33").Value = ComboBox_C2.Value * _
        ComboBox_C3.Value
        End If
        End If
        End If
        
        'Masquer la feuille "VIERGE"
        Sheets("VIERGE").Select
        ActiveWindow.SelectedSheets.Visible = False
            
        'Sélection de la dernière feuille de droite
        Sheets(Sheets.count).Select
        
        Unload Me
        
        'UsF_ICP.Show
        
        End Sub
        0
    4. Ne connaissant ps la structure de votre fichier, ni celle du formulaire , j'ai donc conservé que la partie
      'On copie une fiche vierge
      With Sheets("VIERGE").Select
          Cells.Select
          Application.CutCopyMode = False
          Selection.Copy
          'Sélection du dernier onglet à droite
          Sheets(Sheets.Count).Select
          Range("A1").Select
          Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
          SkipBlanks:=False, Transpose:=False
          ActiveSheet.Paste
          ActiveWindow.Zoom = 70
          Range("C2").Select
          Application.CutCopyMode = False
          
          'Suppression du quadrillage,de la barre de formule et de la barre d'en-tête :
          ActiveWindow.DisplayGridlines = False
          ActiveWindow.DisplayHeadings = False
          Application.DisplayFormulaBar = False
      End With


      et
       'Masquer la feuille "VIERGE"
      Sheets("VIERGE").Select
      ActiveWindow.SelectedSheets.Visible = False


      et ça fonctionne bien, le problème se situe donc après le "End With" et avant le masquage de la feuille.

      Mettez un point d'arrêt après le "End With", lancez la macro et continuez en pas à pas avec F8, vous finirez bien par trouver ce qui cloche.
      0