Macro ne fonctionnant pas en ajoutant une feuille et en la renomant

Bonjour à tous,

Voici ce qui pose probléme quand j'ajoute une feuille et veut la renommer !

le Nom pose probléme !

Sheets.Add(after:=Sheets(Sheets.Count)).Name = Nom

Sub Mot_De_Passe()
Dim Nom As String, i As Long, j As Long, MDP1
With Sheets(1)
Nom = InputBox("Quel est votre NOM ?", "NOM")
For i = 2 To 1000
If UCase(Nom) = .Cells(i, 1) Then
MDP1 = InputBox("Veuillez indiquer votre mot de passe !", "MOT DE PASSE")
For j = 2 To 1000
If UCase(.Cells(j, 2)) = MDP1 Then
On Error Resume Next
Sheets.Add(after:=Sheets(Sheets.Count)).Name = Nom
On Error GoTo 0
Exit Sub
ElseIf .Cells(j, 2) = "" Then
MsgBox " votre Mot de Passe est INCORRECT "
ActiveWorkbook.Close Savechanges:=False
Application.Quit
End If
Next j
ElseIf .Cells(i, 1) = "" Then
MsgBox " il n'y a pas de NOM attribué "
ActiveWorkbook.Close Savechanges:=False
Application.Quit
End If
Next i
End With
End Sub

Le bouton " OUVRIR FEUILLE 1 " > Mot de passe 1234

le bouton " OK " sert à utiliser la macro :

Mot_De_Passe

Merci à l'avance et bonne journée !

8mot-de-passe.zip (19.48 Ko)

Bonjour

Le nom des onglets doit respecter des conditions : pas plus de 31 caractères, pas de caractères spéciaux, etc.

Avec une rechercher sur internet ou dans l'aide vous comprendrez alors peut être le pourquoi du comment...

@ bientôt

LouReeD

Bonjour à tous,

Un essai ...

Sub Mot_De_Passe()
Dim Nom As String, i As Long, j As Long, MDP1
   With Sheets(1)
      Nom = InputBox("Quel est votre NOM ?", "NOM")
      For i = 2 To 10
         If UCase(Nom) = .Cells(i, 1) Then
            MDP1 = InputBox("Veuillez indiquer votre mot de passe !", "MOT DE PASSE")
            For j = 2 To .Cells(Rows.Count, "A")    ' modif ric
               If UCase(.Cells(j, 2)) = MDP1 Then
                  Sheets.Add after:=Sheets(Sheets.Count) ' modif ric
                  Sheets(Sheets.Count).Name = Nom        ' modif ric
                  Exit Sub
               ElseIf .Cells(j, 2) = "" Then
                  MsgBox " votre Mot de Passe est INCORRECT "
                  ActiveWorkbook.Close Savechanges:=False
                  Application.Quit
               End If
            Next j
         ElseIf .Cells(i, 1) = "" Then
            MsgBox " il n'y a pas de NOM attribué "
            ActiveWorkbook.Close Savechanges:=False
            Application.Quit
         End If
      Next i
   End With
End Sub

ric

Re,

Merci pour ta réponse mais ça plante ( flèche jaune)

excel

Bonsoir,

une proposition en indentant le code mais surtout en faisant un test sur la validité du nom proposé, puis en donnant le nom de la feuille par un UpCase :

Sub Mot_De_Passe()
    Dim Nom As String, i As Long, j As Long, MDP1
    With Sheets(1)
        Nom = InputBox("Quel est votre NOM ?", "NOM")
        If vérif_validitée_nom(Nom) = False Then MsgBox ("Erreur dans le nom de feuille proposé !"): Exit Sub
        For i = 2 To 1000
            If UCase(Nom) = .Cells(i, 1) Then
                MDP1 = InputBox("Veuillez indiquer votre mot de passe !", "MOT DE PASSE")
                For j = 2 To 1000
                    If UCase(.Cells(j, 2)) = MDP1 Then
                        On Error Resume Next
                            Sheets.Add(after:=Sheets(Sheets.Count)).Name = UCase(Nom)
                        On Error GoTo 0
                        Exit Sub
                    ElseIf .Cells(j, 2) = "" Then
                        MsgBox " votre Mot de Passe est INCORRECT "
                        ActiveWorkbook.Close Savechanges:=False
                        Application.Quit
                    End If
                Next j
            ElseIf .Cells(i, 1) = "" Then
                MsgBox " il n'y a pas de NOM attribué "
                ActiveWorkbook.Close Savechanges:=False
                Application.Quit
            End If
        Next i
    End With
End Sub

Function vérif_validitée_nom(le_nom As String) As Boolean
    vérif_validitée_nom = True
    Dim NbCar As Long
    NbCar = Len(le_nom)
    If NbCar > 28 Then
        vérif_validitée_nom = False
        Exit Function
    End If
    Dim CPT As Long
    Dim Carac As String
    For CPT = 1 To NbCar
        Carac = Mid(le_nom, CPT, 1)
        If InStr("[]/?*:", Carac) <> 0 Then
            vérif_validitée_nom = False
            Exit For
        End If
    Next CPT
End Function

@ bientôt

LouReeD

Re,

excela

En testant c'est mieux !

Un nouveau code (encore modifié !) :

Sub Mot_De_Passe()
    Dim Nom As String, i As Long, j As Long, MDP1, a
    With Sheets(1)
        Nom = InputBox("Quel est votre NOM ?", "NOM")
        If vérif_validitée_nom(Nom) = False Then MsgBox ("Erreur dans le nom de feuille proposé !"): Exit Sub
        For i = 2 To 1000
            If UCase(Nom) = UCase(.Cells(i, 1)) Then
                MDP1 = InputBox("Veuillez indiquer votre mot de passe !", "MOT DE PASSE")
                If UCase(.Cells(i, 2)) = UCase(MDP1) Then
                    On Error Resume Next
                    Sheets(UCase(Nom)).Activate
                    If Err > 1 Then
                        Sheets.Add after:=Sheets(Worksheets.Count)
                        ActiveSheet.Name = UCase(Nom)
                    Else
                        Sheets(UCase(Nom)).Visible = True
                        Sheets(UCase(Nom)).Activate
                    End If
                    On Error GoTo 0
                    Exit Sub
                Else
                    MsgBox " votre Mot de Passe est INCORRECT "
                    ActiveWorkbook.Close Savechanges:=False
                    Application.Quit
                End If
            End If
        Next i
        MsgBox " il n'y a pas de NOM attribué "
        ActiveWorkbook.Close Savechanges:=False
        Application.Quit
    End With
End Sub

Function vérif_validitée_nom(le_nom As String) As Boolean
    vérif_validitée_nom = True
    Dim NbCar As Long
    NbCar = Len(le_nom)
    If NbCar > 28 Then
        vérif_validitée_nom = False
        Exit Function
    End If
    Dim CPT As Long
    Dim Carac As String
    For CPT = 1 To NbCar
        Carac = Mid(le_nom, CPT, 1)
        If InStr("[]/?*:", Carac) <> 0 Then
            vérif_validitée_nom = False
            Exit For
        End If
    Next CPT
End Function

@ bientôt

LouReeD

Re,

même probléme que précédemment ( le Nom choisi ne s'affiche pas à la place de Feuil3 )

Merci

Bonne soirée !

J'ai modifié le code entre temps...

@ bientôt

LouReeD

Re,

C'est parfait !

Merci beaucoup et bonne nuit !

Rechercher des sujets similaires à "macro fonctionnant pas ajoutant feuille renomant"