Améliorer une macro facile

Résolu
Bonjour,
vous trouvez ci joint un fichier contient une macro qui ajoute un ligne avec certain changement, j'aimerai ajouter une seule modification :
déplacer le F2 vers G2 et F4 vers G4 ainsi de suite...

https://www.cjoint.com/c/FDliDShnrSL

Merci infiniment

3 réponses

  1. Contributeur
    Bonjour,

    Il me semble que tu avais la réponse dans le précédent sujet :
    https://forums.commentcamarche.net/forum/affich-33336372-ameliorer-une-macro#3

    Je t'avais corrigé ta ligne incorrecte
        lstcol = Cells(DerLig, Columns.Count).End(xlToRight).Column
    par
        lstcol = Cells(1, 1).End(xlToRight).Column + 1

    et je vois que tu as repris l'ancienne.

    Voici ta macro corrigée :
    Public Sub ajout_ligne()
    Dim lstcol As Long
    Dim lig As Long, DerLig As Long
        DerLig = Cells(Rows.Count, 1).End(xlUp).Row
        lstcol = Cells(1, 1).End(xlToRight).Column
        MsgBox (lstcol)
        For lig = DerLig To 1 Step -1
            Rows(lig + 1).Insert
            Cells(lig + 1, 1).Resize(, lstcol).Value = Cells(lig, 1).Resize(, lstcol).Value
            Cells(lig + 1, 6).Insert xlToRight
            Cells(lig + 1, 3) = "516100"
        Next lig
    End Sub
    
    1. Si je veux mettre F1 vers G1 et garder F2, c-a-d je fais l'inverse pour l'ordre des ecritures?!
      Merci infiniment
    2. Contributeur
      Non, il te suffit de changer de ligne :
              Cells(lig , 6).Insert xlToRight
      au lieu de
             Cells(lig + 1, 6).Insert xlToRight
      
  2. Mes amis??
  3. COMME ça ?
    Public Sub ajout_ligne()
    Dim lstcol As Long
    Dim lig As Long, DerLig As Long
        DerLig = Cells(Rows.Count, 1).End(xlUp).Row
        lstcol = Cells(DerLig, Columns.Count).End(xlToRight).Column
        MsgBox (lstcol)
        For lig = DerLig To 1 Step -1
            Rows(lig + 1).Insert
            Cells(lig + 1, 1).Resize(, lstcol).Value = Cells(lig, 1).Resize(, lstcol).Value
    '        For Each cll In Cells(lig, 1).Resize(, lstcol)
    '        If cll.Value = "516100" Or cll.Value = "610000" Or cll.Value = "620000" Then
    '        cll.Offset(1, 0).Value = "516100"
    '        Exit For
    '        End If
    '        Next
        Cells(lig + 1, 3) = "516100"
        Next lig
    End Sub
    


    EDIT : Ajout du LANGAGE dans les balises de code (la coloration syntaxique).
    Explications disponibles ici : ICI

    Merci d'y penser dans tes prochains messages.