IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Methode de la classe range a échoué [XL-2003]


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mai 2008
    Messages
    132
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 132
    Par défaut Methode de la classe range a échoué
    Bonjour,

    Je suis en train de mettre en place une fonction mais comme d'habitude, il y a toujours un truc qui cloche ...

    Voici ma fonction au complet :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    Public Function taux_occupation(cl2)
    
        Dim f3 As Worksheet
    
        cl2.Activate
    
        FeuilleExiste = False
    
        'pout toutes les feuilles du classeur "DATE_Etat_Switch" si son nom est égale a celui du Local, FeuilleExiste_
        'devient true sinon il ne change pas
        For Each objFeuille In cl2.Sheets
          If objFeuille.Name = "Taux_d'occupation" Then
            FeuilleExiste = True
            Exit For
          End If
        Next
    
        'si la feuille ayant le nom du local n'existe pas
        If (FeuilleExiste = False) Then
    
            'création d'une feuille
            Sheets.Add
    
            'renomme la feuille avec le nom du local
            ActiveSheet.Name = "Taux_d'occupation"
    
            '*****MISE EN PAGE DU LOCAL*****'
            'pour le titre du local qui est sur la premiere ligne de la feuille
            Range(Cells(1, 7), Cells(1, 12)).Select
            With Selection
                .MergeCells = True
                .Value = "Taux d'occupation des switch"
                .Font.Name = "Arial"
                .Font.Size = 22
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Borders(xlEdgeTop).Weight = xlMedium
                .Borders(xlEdgeLeft).Weight = xlMedium
                .Borders(xlEdgeBottom).Weight = xlMedium
                .Borders(xlEdgeRight).Weight = xlMedium
            End With
    
            'couleur de fond blanc de la cellule A1 à BB500
            Range("A1:BB500").Interior.ColorIndex = 2
    
            Cells(4, 2).Select
            With Selection
                .Value = "Local"
                .Font.Name = "Arial"
                .Font.Size = 14
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Borders(xlEdgeTop).Weight = xlMedium
                .Borders(xlEdgeLeft).Weight = xlMedium
                .Borders(xlEdgeBottom).Weight = xlMedium
                .Borders(xlEdgeRight).Weight = xlMedium
            End With
    
            Cells(4, 3).Select
            With Selection
                .Value = "Switch"
                .Font.Name = "Arial"
                .Font.Size = 14
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Borders(xlEdgeTop).Weight = xlMedium
                .Borders(xlEdgeLeft).Weight = xlMedium
                .Borders(xlEdgeBottom).Weight = xlMedium
                .Borders(xlEdgeRight).Weight = xlMedium
            End With
    
            Range(Cells(4, 4), Cells(4, 6)).Select
            With Selection
                .MergeCells = True
                .Value = "Taux d'occupation"
                .Font.Name = "Arial"
                .Font.Size = 14
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Borders(xlEdgeTop).Weight = xlMedium
                .Borders(xlEdgeLeft).Weight = xlMedium
                .Borders(xlEdgeBottom).Weight = xlMedium
                .Borders(xlEdgeRight).Weight = xlMedium
            End With
            '*****FIN MISE EN PAGE DU LOCAL*****'
    
        End If
    
        Set f3 = cl2.Worksheets("Taux_d'occupation")
    
        cpt_ligne = 5
    
        For i = 1 To Sheets.Count
        
            cpt_vert = 0
            
            If Sheets(i).Name Like "CH*" Then
    
                Sheets(i).Activate
    
                LocalName = Sheets(i).Name
    
                For j = 3 To 100
    
                    If Cells(j, 2).MergeCells = True Then
    
                        SwitchName = Cells(j, 2).Value
    MsgBox LocalName & "  " & SwitchName
    '||||||||||||||||||||
                        For k = j + 1 To j + 2
                            For l = 2 To 27
    
                                If Cells(k, l).Interior.ColorIndex = 4 Or Cells(k, l).Interior.ColorIndex = 45 Then
                                    cpt_vert = cpt_vert + 1
                                End If
    
                            Next l
                        Next k
    
                        taux_vert = (cpt_vert * 100) / 48
    '||||||||||||||||||||
    
    '|||||||||||||||||||
    
                        f3.Cells(cpt_ligne, 2).Value = LocalName
    
                        f3.Cells(cpt_ligne, 3).Select
                        With Selection
                            .Value = SwitchName
                            .Font.Size = 12
                            .HorizontalAlignment = xlCenter
                            .VerticalAlignment = xlCenter
                            .Borders(xlEdgeTop).Weight = xlMedium
                            .Borders(xlEdgeLeft).Weight = xlMedium
                            .Borders(xlEdgeBottom).Weight = xlMedium
                            .Borders(xlEdgeRight).Weight = xlMedium
                        End With
    
                        f3.Range(Cells(cpt_ligne, 4), Cells(cpt_ligne, 6)).Select
                        With Selection
                            .MergeCells = True
                            .Value = taux_vert
                            .Font.Size = 12
                            .HorizontalAlignment = xlCenter
                            .VerticalAlignment = xlCenter
                            .Borders(xlEdgeTop).Weight = xlMedium
                            .Borders(xlEdgeLeft).Weight = xlMedium
                            .Borders(xlEdgeBottom).Weight = xlMedium
                            .Borders(xlEdgeRight).Weight = xlMedium
                        End With
    
                        cpt_ligne = cpt_ligne + 1
    '||||||||||||||||||||
    
                    End If
    
                Next j
    
            End If
    
        Next i
    
    End Function
    Voila ca bogue au niveau de la ligne en rouge, mais pourquoi ??

    Cordialement
    Benjamin

  2. #2
    Expert éminent Avatar de mercatog
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    9 435
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations forums :
    Inscription : Juillet 2008
    Messages : 9 435
    Par défaut
    Une fonction ne permet pas de sélectionner une cellule
    regarde du côté de la différence entre Sub (procédure) et Function (fonction)

  3. #3
    Membre confirmé
    Inscrit en
    Mai 2008
    Messages
    132
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 132
    Par défaut
    Donc je fais la même chose que function sauf que je met sub ?? Ca sera pareil, elle sera appelé et exécuté ??

  4. #4
    Expert éminent Avatar de mercatog
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    9 435
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations forums :
    Inscription : Juillet 2008
    Messages : 9 435
    Par défaut
    J'ai réécris ta Sub en éliminant les lignes superflues
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    Option Explicit
     
    Public Sub Taux_Occupation(ByRef cl2 As Workbook)
     
    Dim f3 As Worksheet, objFeuille As Worksheet
    Dim FeuilleExiste As Boolean
    Dim cpt_ligne As Long, cpt_vert As Long, i As Long, j As Long, k As Long, L As Long
    Dim taux_vert As Double
     
    Dim LocalName As String, SwitchName As String
    With cl2
        'pout toutes les feuilles du classeur "DATE_Etat_Switch" si son nom est égale a celui du Local, FeuilleExiste_
        'devient true sinon il ne change pas
        For Each objFeuille In .Sheets
            If objFeuille.Name = "Taux_d'occupation" Then
                FeuilleExiste = True
                Exit For
            End If
        Next
     
        'si la feuille ayant le nom du local n'existe pas
        If FeuilleExiste <> True Then
            .Sheets.Add.Name = "Taux_d'occupation"
            '*****MISE EN PAGE DU LOCAL*****'
            'pour le titre du local qui est sur la premiere ligne de la feuille
            With .Sheets("Taux_d'occupation")
                With .Range(.Cells(1, 7), .Cells(1, 12))
                    .MergeCells = True
                    .Value = "Taux d'occupation des switch"
                    .Font.Name = "Arial"
                    .Font.Size = 22
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .Borders.Weight = xlMedium
                End With
     
                'couleur de fond blanc de la cellule A1 à BB500
                .Range("A1:BB500").Interior.ColorIndex = 2
     
                With .Cells(4, 2)
                    .Value = "Local"
                    .Font.Name = "Arial"
                    .Font.Size = 14
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .Borders.Weight = xlMedium
                End With
     
                With .Cells(4, 3)
                    .Value = "Switch"
                    .Font.Name = "Arial"
                    .Font.Size = 14
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .Borders.Weight = xlMedium
                End With
     
                With .Range(.Cells(4, 4), .Cells(4, 6))
                    .MergeCells = True
                    .Value = "Taux d'occupation"
                    .Font.Name = "Arial"
                    .Font.Size = 14
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .Borders.Weight = xlMedium
                End With
            End With
        '*****FIN MISE EN PAGE DU LOCAL*****'
        End If
     
        Set f3 = .Sheets("Taux_d'occupation")
        cpt_ligne = 5
        For i = 1 To .Sheets.Count
            cpt_vert = 0
            If .Sheets(i).Name Like "CH*" Then
                With .Sheets(i)
                    LocalName = .Name
                    For j = 3 To 100
                        If .Cells(j, 2).MergeCells = True Then
                            SwitchName = .Cells(j, 2).Value
                            MsgBox LocalName & "  " & SwitchName
                            For k = j + 1 To j + 2
                                For L = 2 To 27
                                    If .Cells(k, L).Interior.ColorIndex = 4 Or .Cells(k, L).Interior.ColorIndex = 45 Then
                                        cpt_vert = cpt_vert + 1
                                    End If
                                Next L
                            Next k
                            taux_vert = (cpt_vert * 100) / 48
                            f3.Cells(cpt_ligne, 2).Value = LocalName
                            With f3.Cells(cpt_ligne, 3)
                                .Value = SwitchName
                                .Font.Size = 12
                                .HorizontalAlignment = xlCenter
                                .VerticalAlignment = xlCenter
                                .Borders.Weight = xlMedium
                            End With
                            With f3.Range(f3.Cells(cpt_ligne, 4), f3.Cells(cpt_ligne, 6))
                                .MergeCells = True
                                .Value = taux_vert
                                .Font.Size = 12
                                .HorizontalAlignment = xlCenter
                                .VerticalAlignment = xlCenter
                                .Borders.Weight = xlMedium
                            End With
                            cpt_ligne = cpt_ligne + 1
                        End If
                    Next j
                End With
            End If
        Next i
    End With
    End Sub
    tu peux l'appeler
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Taux_Occupation ThisWorkbook 'ou call Taux_Occupation(ThisWorkbook)
    Cette procédure à pour paramètre un workbook

    Edit, corrections du code

  5. #5
    Membre confirmé
    Inscrit en
    Mai 2008
    Messages
    132
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 132
    Par défaut
    Merci je l'ai transformer en procédure, mais cela me fais toujours la même erreur, les cells(...) passe mais les range(...) non ...

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Février 2006
    Messages
    288
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 288
    Par défaut
    Bonjour,
    La sélection d'une plage ne fonctionne que si sa feuille est la feuille active.
    Dans ton cas tu as un peu plus haut Sheets(i).Activate. Donc ce n'est pas toujours f3 qui est la feuille active, d'où probablement le plantage.

    Edit : teste le code de Mercatog, il ne fait pas de select et c'est mieux.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. La méthode sort de la classe range a échoué
    Par laetitia02 dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 28/05/2010, 11h09
  2. La Méthode Activate de la classe Range a échoué
    Par Amiral19 dans le forum Macros et VBA Excel
    Réponses: 13
    Dernier message: 06/07/2007, 15h46
  3. La méthode pastespecial de la classe range a échoué
    Par heyhey62 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 05/07/2007, 18h52
  4. Réponses: 13
    Dernier message: 29/06/2007, 19h03
  5. la methode select de la classe range a échoué
    Par shrekos007 dans le forum Macros et VBA Excel
    Réponses: 31
    Dernier message: 26/05/2007, 16h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo