Problème de nom de feuille [VBA]

Résolu/Fermé
mimi_1990 Messages postés 75 Date d'inscription jeudi 1 juillet 2010 Statut Membre Dernière intervention 15 novembre 2010 - 21 juil. 2010 à 14:44
mimi_1990 Messages postés 75 Date d'inscription jeudi 1 juillet 2010 Statut Membre Dernière intervention 15 novembre 2010 - 21 juil. 2010 à 15:24
Bonjour, (Le code parait long mais j'ai tout copié lisez l'intro s'il vous plait !)

J'ai créé ce programme qui compile les résultats de la comparaison de deux feuilles dans laquelle j'ai au préalable importer des fichiers !
Mon souci est que mon utilisateur lance une comparaison et comme vous pouvez le voir a la fin du code la Feuil3 est renommée en Synthèse...
Mon utilisateur est censé faire des modifications et relancer la comparaison ensuite...
Seulement une fois que la feuil3 a été renommée en synthèse il m'est impossible de l'appeler a nouveau ...
J'aimerai savoir comment faire pour renommer ma feuille comme il faut car si je renomme Synthèse en feuil3 en début de code il me dit la premiere fois qu'il ne trouve pas synthèse... Logique !



Private Sub CompareSheets()
'compare sheets Reel/Theorique
    Dim cherche As Range
    Dim valeur As String
    Dim i, derlig, An, numlig As Integer
    Dim CodeAetG As Double
    Dim Ptrim As Currency
    Sheets("Feuil3").Range("B2:B7").Value = ""
        With Sheets("Réel")
            derlig = .Cells(65536, 1).End(xlUp).row
        End With
    For i = 1 To derlig
        With Sheets("Réel")
            valeur = .Cells(i, 1).Value
        End With
        With Sheets("Théorique").Columns(1)
            Set cherche = .Cells.Find(valeur)
                If cherche Is Nothing Then
                    'MsgBox "Feuille Réel: Le contrat " & valeur & " (ligne" & " " & i & ")" & " n'a pas été trouvé"
                    Sheets("Feuil3").Select
                    Range("B2").Select
                    ActiveCell.Value = ActiveCell.Value & vbLf & valeur & " (ligne " & i & ")"
                Else
                'Compare ClientID, Trim Price, Bank informations
                    Sheets("Réel").Select
                    numlig = cherche.row
                    CodeAetG = Sheets("Réel").Cells(i, 18).Value * 10 ^ 5 + Sheets("Réel").Cells(i, 19).Value
                    Ptrim = Sheets("Réel").Cells(i, 26).Value / 100
                    
                    If Sheets("Réel").Cells(i, 2).Value <> Val(Sheets("Théorique").Cells(numlig, 2).Value) Then
                        'MsgBox "Feuille Réel: Le code Client" & " (ligne" & " " & i & ")" & " est différent"
                        Sheets("Feuil3").Select
                        Range("B3").Select
                        ActiveCell.Value = ActiveCell.Value & vbLf & Sheets("Réel").Cells(i, 2).Value & " (ligne " & i & ")"
                        
                        
                        If Ptrim <> Val(Sheets("Théorique").Cells(numlig, 13).Value) Then
                        'MsgBox "Feuille Réel: Le Prix Trimestriel T.T.C." & " (ligne" & " " & i & ")" & " est différent"
                            Sheets("Feuil3").Select
                            Range("B6").Select
                            ActiveCell.Value = ActiveCell.Value & vbLf & Ptrim & " (ligne " & i & ")"
                            
                            If CodeAetG <> Val(Sheets("Théorique").Cells(numlig, 3).Value) Then
                                'MsgBox "Feuille Réel: Le Code Agence/Guichet" & " (ligne" & " " & i & ")" & " est différent"
                                Sheets("Feuil3").Select
                                Range("B4").Select
                                ActiveCell.Value = ActiveCell.Value & vbLf & CodeAetG & " (ligne " & i & ")"
                                
                                
                                If Sheets("Réel").Cells(i, 20).Value <> Val(Sheets("Théorique").Cells(numlig, 4).Value) Then
                                'MsgBox "Feuille Réel: Le Numéro de compte" & " (ligne" & " " & i & ")" & " est différent"
                                Sheets("Feuil3").Select
                                Range("B5").Select
                                ActiveCell.Value = ActiveCell.Value & vbLf & Sheets("Réel").Cells(i, 20).Value & " (ligne " & i & ")"
                                End If
                            End If
                        End If
                    End If
                End If
        End With
     Sheets("Feuil3").Select
     Range("B7").Select
     ActiveCell.Value = Application.Sum(Sheets("Réel").Range("H1").EntireColumn)

     Next i
    'compare sheets Theorique/Reel
    Dim search As Range
    Dim valeur2 As Variant
    Dim j, derlig2, numlig2 As Integer
       With Sheets("Théorique")
            derlig2 = .Cells(65536, 1).End(xlUp).row
        End With
    Sheets("Feuil3").Range("C2:C7").Value = ""
    For j = 2 To derlig2
        With Sheets("Théorique")
            valeur2 = .Cells(j, 1).Value
        End With
        With Sheets("Réel").Columns(1)
            Set search = .Cells.Find(valeur2)
                If search Is Nothing Then
                'MsgBox "Feuille Théorique: Le contrat" & valeur2 & " (ligne" & " " & j & ")" & " n'a pas été trouvé"
                    Sheets("Feuil3").Select
                    Range("C2").Select
                    ActiveCell.Value = ActiveCell.Value & vbLf & valeur2 & " (ligne " & j & ")"
                Else
                'Compare ClientID, Trim Price, Bank informations
                   Sheets("Théorique").Select
                    numlig2 = search.row
                    CodeAetG = Sheets("Réel").Cells(i, 18).Value * 10 ^ 5 + Sheets("Réel").Cells(i, 19).Value
                    Ptrim = Sheets("Réel").Cells(i, 26).Value / 100
                   
                   If Val(Sheets("Théorique").Cells(j, 2).Value) <> Sheets("Réel").Cells(numlig2, 2).Value Then
                        'MsgBox "Feuille Théorique: Le code Client" & " (ligne" & " " & j & ")" & " est différent"
                            Sheets("Feuil3").Select
                            Range("C3").Select
                            ActiveCell.Value = ActiveCell.Value & vbLf & Val(Sheets("Théorique").Cells(j, 2).Value) & " (ligne " & j & ")"
                        
                        
                        If Val(Sheets("Théorique").Cells(j, 13).Value) <> Ptrim Then
                        'MsgBox "Feuille Théorique: Le Prix Trimestriel T.T.C." & " (ligne" & " " & j & ")" & " est différent"
                            Sheets("Feuil3").Select
                            Range("C6").Select
                            ActiveCell.Value = ActiveCell.Value & vbLf & Val(Sheets("Théorique").Cells(j, 13).Value) & " (ligne " & j & ")"
                            
                            
                            If Val(Sheets("Théorique").Cells(j, 3).Value) <> CodeAetG Then
                                'MsgBox "Feuille Théorique: Le Code Agence/Guichet" & " (ligne" & " " & j & ")" & " est différent"
                                Sheets("Feuil3").Select
                            Range("C4").Select
                            ActiveCell.Value = ActiveCell.Value & vbLf & Val(Sheets("Théorique").Cells(j, 3).Value) & " (ligne " & j & ")"
                                
                               
                                If Val(Sheets("Théorique").Cells(j, 4).Value) <> Sheets("Réel").Cells(numlig2, 20).Value Then
                                'MsgBox "Feuille Théorique: Le Numéro de compte" & " (ligne" & " " & j & ")" & " est différent"
                                    Sheets("Feuil3").Select
                                    Range("C5").Select
                                    ActiveCell.Value = ActiveCell.Value & vbLf & Val(Sheets("Théorique").Cells(j, 4).Value) & " (ligne " & j & ")"
                                
                                
                                
                                End If
                            End If
                        End If
                    End If
                End If
        End With
        Sheets("Feuil3").Select
        Range("C7").Select
        ActiveCell.Value = Application.Sum(Sheets("Théorique").Range("M1").EntireColumn)
        Next j
        Sheets("Feuil3").Select
        Range("B1").Select
        ActiveCell.FormulaR1C1 = "Réel"
    Range("C1").Select
        ActiveCell.FormulaR1C1 = "Théorique"
    Range("A2").Select
        ActiveCell.FormulaR1C1 = "Contrats non trouvé (Valeur + Ligne)"
    Range("A3").Select
        ActiveCell.FormulaR1C1 = "Codes Clients Différents (Valeur + Ligne)"
    Range("A4").Select
        ActiveCell.FormulaR1C1 = "Code Agence/Guichet différent (Valeur + Ligne)"
    Range("A5").Select
        ActiveCell.FormulaR1C1 = "Numéro de compte différent (Valeur + Ligne)"
    Range("A6").Select
        ActiveCell.FormulaR1C1 = "prix trimestriel différent (Valeur + Vigne)"
    Range("A7").Select
        ActiveCell.FormulaR1C1 = "Somme des prélèvements"
          Range("B1:C1").Select
    With selection.Interior
        .ColorIndex = 48
        .Pattern = xlSolid
    End With
        Range("A2:A7").Select
    With selection.Interior
        .ColorIndex = 48
        .Pattern = xlSolid
    End With
       
         
        Range("A2:C7,B1:C1").Select
    selection.Borders(xlDiagonalDown).LineStyle = xlNone
    selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
     Rows("1:8").EntireRow.AutoFit
     Columns("A:C").EntireColumn.AutoFit
     Sheets("Feuil3").Name = "Synthèse"
    End Sub



J'aimerai également savoir comment mettre dans mon programme comparesheets au tout début une msgbox au cas ou les feuilles Réel et Théorique nexisterait pas, disant:
" veuillez au préalable importez un fichier Réel et un fichier Théorique !"


Davance merci pour vos réponses !

2 réponses

pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 744
21 juil. 2010 à 15:00
Bonjour,
1ère question : Quelque soit son nom, Feuil3 ou synthese ou peu importe, les feuilles excel sont également répertoriées sous VBA par leur numéro. regarde les noms qui apparaissent en haut à gauche dans la fenêtre Visual Basic :
Feuil1(Feuil1)
Feuil2(Feuil2)
Feuil3(Feuil3)
Après avoir renommé ta feuille3 tu as :
Feuil1(Feuil1)
Feuil2(Feuil2)
Feuil3(synthèse)
Suffit donc de toujours appeler ta 3ème feuille par son numéro soit :
Sheets(3)
au lieu de Sheets("Feuil3") ou Sheets("synthese")

2ème question : Un test en début de macro, un peu comme ceci :
Dim Ws As Worksheet
Dim test, test1 As String
test = ""
test1 = ""
    For Each Ws In ThisWorkbook.Worksheets
        If Ws.Name = "Réel" Then 
              test = "OK"
        ElseIf Ws.Name = "Théorique" Then
              test1 = "OK"
        End If
    Next Ws
If test <> "OK" Then 
MsgBox " veuillez au préalable importez un fichier Réel"
Exit Sub
End If
If test1 <> "OK" Then 
MsgBox " veuillez au préalable importez un fichier Théorique"
Exit Sub
End If
0
mimi_1990 Messages postés 75 Date d'inscription jeudi 1 juillet 2010 Statut Membre Dernière intervention 15 novembre 2010 15
21 juil. 2010 à 15:24
c'est nickel merci beaucoup tout fonctionne désormais !
0