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 :

Simplification code VBA généré par enregistreur de macro [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é
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    96
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Juillet 2009
    Messages : 96
    Par défaut Simplification code VBA généré par enregistreur de macro
    Bonjour à toutes et à tous

    J'ai utilsé l'enregistreur de macro pour mettre une mise en forme automatique, des lignes, centrer des données et ajouter un motif sur une cellule.
    Je trouve que le code généré est long, je me demandais s'il n'y avait pas une manière plus élégante de coder tout ça.

    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
     ActiveCell.FormulaR1C1 = "M"
        Range("B100").Select
        ActiveCell.FormulaR1C1 = "S"
        Range("B101").Select
        ActiveCell.FormulaR1C1 = "N"
        Range("B99:B101").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        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
        Range("B100").Select
        With Selection.Interior
            .ColorIndex = 0
            .Pattern = xlGray8
            .PatternColorIndex = xlAutomatic
        End With
        Range("A99:W101").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With

    Merci pour votre aide,

    Bonne journée

  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
    Ceci suffira?
    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
        Range("B99").Value = "M"
        Range("B100").Value = "S"
        Range("B101").Value = "N"
        With Range("B99:B101")
            .HorizontalAlignment = xlCenter
            .Borders(xlEdgeLeft).Weight = xlThin
            .Borders(xlEdgeRight).Weight = xlThin
            .Borders(xlEdgeTop).Weight = xlThin
            .Borders(xlEdgeBottom).Weight = xlThin
            .Borders(xlInsideVertical).Weight = xlThin
            .Borders(xlInsideHorizontal).Weight = xlThin
        End With
        With Range("A99:W101")
            .Borders(xlEdgeLeft).Weight = xlMedium
            .Borders(xlEdgeRight).Weight = xlMedium
            .Borders(xlEdgeTop).Weight = xlMedium
            .Borders(xlEdgeBottom).Weight = xlMedium
        End With
     
        With Range("B100").Interior
            .ColorIndex = 0
            .Pattern = xlGray8
        End With

  3. #3
    Membre Expert
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    652
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 652
    Par défaut
    Bonjour,

    Voyez si ce code vous convient mieux

    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
     
    Sub aa()
    Dim R As Range
    Dim i&
    [b99] = "M"
    [b100] = "S"
    [b101] = "N"
    Set R = Range("B99:B101")
    With R
      .HorizontalAlignment = xlCenter
      On Error Resume Next
      For i& = xlDiagonalDown To xlDiagonalUp
        .Borders(i&).LineStyle = xlNone
      Next i&
      For i& = xlEdgeLeft To xlInsideHorizontal
        With .Borders(i&)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
        End With
      Next i&
      On Error GoTo 0
    End With
    With [b100].Interior
      .ColorIndex = 0
      .Pattern = xlGray8
      .PatternColorIndex = xlAutomatic
    End With
    Set R = Range("A99:W101")
    With R
      For i& = xlDiagonalDown To xlDiagonalUp
        .Borders(i&).LineStyle = xlNone
      Next i&
      For i& = xlEdgeLeft To xlEdgeRight
        With .Borders(i&)
          .LineStyle = xlContinuous
          .Weight = xlMedium
          .ColorIndex = xlAutomatic
        End With
      Next i&
    End With
    End Sub
    Cordialement.

    PMO
    Patrick Morange

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    96
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Juillet 2009
    Messages : 96
    Par défaut
    Re

    Merci pour ta proposition PMO.

    J'ai juste une autre petite question.
    Est ce possible d'assigner à plusieur plage la même mise en forme.
    Je pensais marquer quelquechose comme ça, mais visiblement cela ne marche pas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Set R = Range(Cells(5, 4), Cells(7, 4))
    Set R1 = Range(Cells(5, 6), Cells(7, 6))
     
    With R;R1
        .Borders(xlEdgeLeft).Weight = xlThin
    End With
    Merci

  5. #5
    Membre Expert Avatar de Krovax
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 888
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 888
    Par défaut
    bonjour essaye
    je n'ai pas excel sous la main pour tester du coup je ne sas plus si c'est une , ou un ; je te laisse consulter l'aide

  6. #6
    Membre Expert Avatar de Fvandermeulen
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2007
    Messages
    1 869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 869
    Par défaut
    Salut,
    Dans ce cas tu peux faire:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    With Range("D5:D7,F5:F7")
        .Borders(xlEdgeLeft).Weight = xlThin
    End With
    Par contre j'ai jamais essayé avec la "méthode" cells, à voir si c'est utile...

    A+

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    96
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Juillet 2009
    Messages : 96
    Par défaut
    Merci à tous

    me convient parfaitement


    @Fvandermeulen: ta solution ne fonctionne pas chez moi.
    En effet je voudrais voir apparaitre 2 lignes distinctes une de D5 à D7 et la 2nde de F7 à F5 or avec ton code il n'y a que la 1ere.
    Je veux utiliser cells pour pouvoir par la suite y mettre quelquechose comme

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Set R = Range(Cells(i, 4), Cells(i+2, 4))
    Set R1 = Range(Cells(i, 6), Cells(i+2, 6))
    Il semblerait qu'avec Range cela ne soit pas possible.

    Merci pour votre aide.

    Bonne journée

  8. #8
    Membre Expert
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    652
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 652
    Par défaut
    Bonjour,

    La solution de Fvandermeulen fonctionne. Voici un exemple où la plage ''D5 : D7, F5 : F7'' est colorée en rouge

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Sub bb_Fvandermeulen()
    Dim R As Range
    Set R = Range("D5:D7,F5:F7")
    R.Interior.ColorIndex = 3
    MsgBox R.Address(False, False)
    End Sub
    Le même résultat (mais colorisation en bleu) est obtenu avec

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    Sub aa_pmo()
    Dim R As Range
    Dim i&
    i& = 5
    Set R = Range(Cells(i&, 4), Cells(i& + 2, 4))
    Set R = Application.Union(R, Range(Cells(i, 6), Cells(i + 2, 6)))
    R.Interior.ColorIndex = 5
    MsgBox R.Address(False, False)
    End Sub
    Cordialement.

    PMO
    Patrick Morange

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

Discussions similaires

  1. Intégration du code C généré par la commande mcc
    Par fes2010 dans le forum MATLAB
    Réponses: 5
    Dernier message: 27/08/2008, 08h43
  2. Récupérer code html généré par une jsp
    Par axel119 dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 11/09/2007, 16h17
  3. evenement onclick dans du code html généré par js
    Par gelko dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 15/11/2006, 09h24
  4. [MySQL] Récupérer Code HTML généré par PHP
    Par @ngelofdeath dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 26/04/2006, 21h51
  5. Réponses: 12
    Dernier message: 20/04/2006, 15h15

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