Cherche aide pour simplifier une macro

INFINITY100

XLDnaute Occasionnel
Bonsoir tout le monde

Voila j'ai la macro ci-dessous créée par un débutant que je trouve un peu longue du coup je cherche à la rendre simple pour quel soit assez facile à comprendre

voici la macro

Code:
Sub auto_open()

    Sheets("Bilan").Select
    
    Application.ScreenUpdating = False
    Sheets("Bilan").Unprotect Password:="123456789"
    Range("B6").Select
    ActiveCell.FormulaR1C1 = "=SUM(BD!R[-4]C[6]:R[1048570]C[6])"
    Range("B8").Select
    ActiveCell.FormulaR1C1 = "=SUM(encaissement!R[-6]C:R[1048568]C)"
    Range("B10").Select
    ActiveCell.FormulaR1C1 = "=SUM(Creances!R[-8]C[3]:R[1048566]C[3])"
    Range("B12").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(SUM(R[-6]C[2]:R[-2]C[2])>SUM(R[-6]C:R[-2]C),SUM(R[-6]C[2]:R[-2]C[2])-SUM(R[-6]C:R[-2]C),0)"
        
    Range("D6").Select
    ActiveCell.FormulaR1C1 = "=SUM(BD!R[-4]C[2]:R[1048570]C[2])"
    Range("D8").Select
    ActiveCell.FormulaR1C1 = "=SUM(Charges!R[-6]C:R[1048568]C)"
    Range("D10").Select
    ActiveCell.FormulaR1C1 = "=SUM(Dettes!R[-8]C[1]:R[1048566]C[1])"
    Range("D12").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(SUM(R[-6]C[-2]:R[-2]C[-2])>SUM(R[-6]C:R[-2]C),SUM(R[-6]C[-2]:R[-2]C[-2])-SUM(R[-6]C:R[-2]C),0)"
    Range("B14").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
    Range("D14").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
    Range("A15:D15").Select
    Sheets("Feuille démarrage").Select
    
    Sheets("Bilan").Protect Password:="123456789"
    
    Sheets("Divers").Select
    
    Application.ScreenUpdating = False
    Sheets("Divers").Unprotect Password:="123456789"
    Range("A2").Select
        ActiveCell.FormulaR1C1 = "=TODAY()"
    Range("B2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(SUM(BD!R2C6:R1048576C6)>0,SUM(BD!R2C6:R1048576C6),0)"
    Range("C2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(SUM(BD!R2C8:R1048576C8)>0,SUM(BD!R2C8:R1048576C8),0)"
    Range("D2").Select
        ActiveCell.FormulaR1C1 = "=IF((R2C3-R2C2)<>0,(R2C3-R2C2),0)"
    Range("E2").Select
        ActiveCell.FormulaR1C1 = "=IF(R2C2<>0,AVERAGE(BD!R2C10:R1048576C10),0)"
    Range("F2").Select
        ActiveCell.FormulaR1C1 = _
             "=SUMIF(Dettes!R2C5:R1048576C5,"">=0"",Dettes!R2C5:R1048576C5)"
    Range("G2").Select
        ActiveCell.FormulaR1C1 = _
            "=SUMIF(Dettes!R2C5:R1048576C5,""<=0"",Dettes!R2C5:R1048576C5)"
    Range("H2").Select
        ActiveCell.FormulaR1C1 = "=R2C6+R2C7"
    Range("I2").Select
        ActiveCell.FormulaR1C1 = _
            "=SUMIF(Creances!R2C5:R1048576C5,"">=0"",Dettes!R2C5:R1048576C5)"
    Range("J2").Select
        ActiveCell.FormulaR1C1 = _
            "=SUMIF(Creances!R2C5:R1048576C5,""<=0"",Dettes!R2C5:R1048576C5)"
    Range("K2").Select
        ActiveCell.FormulaR1C1 = "=R2C9+R2C10"
    Range("L2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(SUM(Charges!R2C4:R1048576C4)<>0,SUM(Charges!R2C4:R1048576C4),0)"
    Range("P2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(SUM(encaissement!R2C2:R1048576C2)<>0,SUM(encaissement!R2C2:R1048576C2),0)"
    Range("S2").Select
        ActiveCell.FormulaR1C1 = "=R2C3-R2C16"
    Range("T2").Select
        ActiveCell.FormulaR1C1 = "=IF(Bilan!R12C2<>"""",Bilan!R12C2,0)"
    Range("U2").Select
        ActiveCell.FormulaR1C1 = "=IF(Bilan!R12C4<>"""",Bilan!R12C4,0)"
    Range("V2").Select
        ActiveCell.FormulaR1C1 = "=R2C3+R2C16+R2C11+R2C20"
    Range("W2").Select
    ActiveCell.FormulaR1C1 = "=R2C2+R2C12+R2C8+R2C21"

    Sheets("Divers").Protect Password:="123456789"
    Menu.Show
    Application.ScreenUpdating = True
    
    Range("A1").Select
End Sub

Merci pour votre aide

Cordialement
 

INFINITY100

XLDnaute Occasionnel
Re : Cherche aide pour simplifier une macro

Oui oui Victor je l'est lu et je pense que tu as raison
.Range("D2").FormulaR1C1 ="=R2C3-R2C2"
suffit largement

Et pour la transformation de la macro selon la proposition de Dranreb voici ce que j'ai pu faire à toi de juger mes erreurs :)

Code:
Sub auto_open()
    Application.ScreenUpdating = False
    With Sheets("Bilan")
        .Unprotect Password:="123456789"
        .Range("B6").FormulaR1C1 = "=SUM(BD!R[-4]C[6]:R[1048570]C[6])"
        .Range("B8").FormulaR1C1 = "=SUM(encaissement!R[-6]C:R[1048568]C)"
        .Range("B10").FormulaR1C1 = "=SUM(Creances!R[-8]C[3]:R[1048566]C[3])"
        .Range("B12").FormulaR1C1 = _
        "=MAX(SUM(R[-6]C[2]:R[-2]C[2])-SUM(R[-6]C:R[-2]C),0)"
        .Range("D6").FormulaR1C1 = "=SUM(BD!R[-4]C[2]:R[1048570]C[2])"
        .Range("D8").FormulaR1C1 = "=SUM(Charges!R[-6]C:R[1048568]C)"
        .Range("D10").FormulaR1C1 = "=SUM(Dettes!R[-8]C[1]:R[1048566]C[1])"
        .Range("D12").FormulaR1C1 = _
        "=MAX(SUM(R[-6]C[-2]:R[-2]C[-2])-SUM(R[-6]C:R[-2]C),0)"
        .Range("B14").FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
        .Range("D14").FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
        .Protect Password:="123456789"
    End With
    With Sheets("Divers")
        .Unprotect Password:="123456789"
        .Range("A2").FormulaR1C1 = "=TODAY()"
        .Range("B2").FormulaR1C1 = _
        "=MAX(SUM(BD!R2C6:R1048576C6),0)"
        .Range("C2").FormulaR1C1 = _
        "=MAX(SUM(BD!R2C8:R1048576C8),0)"
        .Range("D2").FormulaR1C1 ="=R2C3-R2C2"
        .Range("E2").FormulaR1C1 = "=IF(R2C2<>0,AVERAGE(BD!R2C10:R1048576C10),0)"
        .Range("F2").FormulaR1C1 = _
        "=MAX(SUM(Dettes!R2C5:R1048576C5),"">=0)"
        .Range("G2").FormulaR1C1 = _
        "=MAX(SUM(Dettes!R2C5:R1048576C5,""<=0)"
        .Range("H2").FormulaR1C1 = "=R2C6+R2C7"
        .Range("I2").FormulaR1C1 = _
        "=MAX(SUM(Creances!R2C5:R1048576C5,"">=0)"
        .Range("J2").FormulaR1C1 = _
        "=MAX(SUM(Creances!R2C5:R1048576C5,""<=0"")"
        .Range("K2").FormulaR1C1 = "=R2C9+R2C10"
        .Range("L2").FormulaR1C1 = _
        "=MAX(SUM(Charges!R2C4:R1048576C4),0)"
        .Range("P2").FormulaR1C1 = _
        "=MAX(SUM(encaissement!R2C2:R1048576C2),0)"
        .Range("S2").FormulaR1C1 = "=R2C3-R2C16"
        .Range("T2").FormulaR1C1 = "=MAX(SUM(Bilan!R12C2,0)"
        .Range("U2").FormulaR1C1 = "=MAX(SUM(Bilan!R12C4,0)"
        .Range("V2").FormulaR1C1 = "=R2C3+R2C16+R2C11+R2C20"
        .Range("W2").FormulaR1C1 = "=R2C2+R2C12+R2C8+R2C21"
        
.Protect Password:="123456789"
    End With
   
    Menu.Show
    Application.ScreenUpdating = True
End Sub
 

INFINITY100

XLDnaute Occasionnel
Re : Cherche aide pour simplifier une macro

Bonsoir Victor

Oui mon ami je l'est testé sur Exemple et l'USF Menu est présent également voici le fichier joint
 

Pièces jointes

  • Exemple.xlsm
    49.7 KB · Affichages: 29
  • Exemple.xlsm
    49.7 KB · Affichages: 32
  • Exemple.xlsm
    49.7 KB · Affichages: 34

Victor21

XLDnaute Barbatruc
Re : Cherche aide pour simplifier une macro

Re,

Désolé INFINITY100. Je donne ma main au chat, et je passe la langue à d'autres plus VBAistes et plus compétents que moi. (ou l'inverse :) )
Mais si vous voulez optimiser l'aide que les contributeurs vous apporteront, ne vous arrêtez pas à :
elle génère une erreur quelqu'un pourrai me la corriger ?
Pensez à indiquer le message d'erreur, et la ligne sur laquelle celle-ci se produit.
 

INFINITY100

XLDnaute Occasionnel
Re : Cherche aide pour simplifier une macro

Merci Victor pour toutes vos réponses et aide aussi ainsi que les autres

Voila à bon entendeur et plus VBAistes :) la macro génère une erreur à la ligne

.Range("F2").FormulaR1C1 = _
"=MAX(SUM(Dettes!R2C5:R1048576C5),"">=0)"

espérant que quelqu'un trouve la solution ou plus la bonne macro

merci
 

Dranreb

XLDnaute Barbatruc
Re : Cherche aide pour simplifier une macro

Bonsoir.

Il me semble que cette formule était à l'origine une =SUMIF. Je n'avais vu de simplifications possibles que pour certaines =IF.
Mais si j'en juge par vos tentative empiriques, vous ne comprenez rien à rien ! Vous ne cherchez même pas à comprendre et vous vous en foutez de ne pas comprendre ! C'est en tout cas vraiment l'impression que j'ai.
 
Dernière édition:

INFINITY100

XLDnaute Occasionnel
Re : Cherche aide pour simplifier une macro

Bonsoir Dranreb

Non c'est pas question que je m’en fou ou je ne cherche pas à comprendre car si c’était le cas j'aurai pas essayer plusieurs fois réaliser la macro voulu seulement c'est juste que je suis débutant et il faut pas trop m'en vouloir mais bon après correction j'ai réalisé la macro et après essai elle fonction très bien :)

Voici le code

Code:
Sub auto_open()
    Application.ScreenUpdating = False
    With Sheets("Bilan")
        .Unprotect Password:="123456789"
        .Range("B6").FormulaR1C1 = "=SUM(BD!R[-4]C[6]:R[1048570]C[6])"
        .Range("B8").FormulaR1C1 = "=SUM(encaissement!R[-6]C:R[1048568]C)"
        .Range("B10").FormulaR1C1 = "=SUM(Creances!R[-8]C[3]:R[1048566]C[3])"
        .Range("B12").FormulaR1C1 = _
        "=MAX(SUM(R[-6]C[2]:R[-2]C[2])-SUM(R[-6]C:R[-2]C),0)"
        .Range("D6").FormulaR1C1 = "=SUM(BD!R[-4]C[2]:R[1048570]C[2])"
        .Range("D8").FormulaR1C1 = "=SUM(Charges!R[-6]C:R[1048568]C)"
        .Range("D10").FormulaR1C1 = "=SUM(Dettes!R[-8]C[1]:R[1048566]C[1])"
        .Range("D12").FormulaR1C1 = _
        "=MAX(SUM(R[-6]C[-2]:R[-2]C[-2])-SUM(R[-6]C:R[-2]C),0)"
        .Range("B14").FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
        .Range("D14").FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
        .Protect Password:="123456789"
        
    End With
    
    With Sheets("Divers")
        .Unprotect Password:="123456789"
        .Range("A2").FormulaR1C1 = "=TODAY()"
        .Range("B2").FormulaR1C1 = _
        "=MAX(SUM(BD!R2C6:R1048576C6),0)"
        .Range("C2").FormulaR1C1 = _
        "=MAX(SUM(BD!R2C8:R1048576C8),0)"
        .Range("D2").FormulaR1C1 = "=IF((R2C3-R2C2)<>0,(R2C3-R2C2),0)"
        .Range("E2").FormulaR1C1 = "=IF(R2C2<>0,AVERAGE(BD!R2C10:R1048576C10),0)"
        .Range("F2").FormulaR1C1 = _
        "=SUMIF(Dettes!R2C5:R1048576C5,"">=0"",Dettes!R2C5:R1048576C5)"
        .Range("G2").FormulaR1C1 = _
        "=SUMIF(Dettes!R2C5:R1048576C5,""<=0"",Dettes!R2C5:R1048576C5)"
        .Range("H2").FormulaR1C1 = "=R2C6+R2C7"
        .Range("I2").FormulaR1C1 = _
        "=SUMIF(Creances!R2C5:R1048576C5,"">=0"",Dettes!R2C5:R1048576C5)"
        .Range("J2").FormulaR1C1 = _
        "=SUMIF(Creances!R2C5:R1048576C5,""<=0"",Dettes!R2C5:R1048576C5)"
        .Range("K2").FormulaR1C1 = "=R2C9+R2C10"
        .Range("L2").FormulaR1C1 = _
        "=MAX(SUM(Charges!R2C4:R1048576C4),0)"
        .Range("P2").FormulaR1C1 = _
        "=MAX(SUM(encaissement!R2C2:R1048576C2),0)"
        .Range("S2").FormulaR1C1 = "=R2C3-R2C16"
        .Range("T2").FormulaR1C1 = "=IF(Bilan!R12C2<>"""",Bilan!R12C2,0)"
        .Range("U2").FormulaR1C1 = "=IF(Bilan!R12C4<>"""",Bilan!R12C4,0)"
        .Range("V2").FormulaR1C1 = "=R2C3+R2C16+R2C11+R2C20"
        .Range("W2").FormulaR1C1 = "=R2C2+R2C12+R2C8+R2C21"
        .Protect Password:="123456789"
    End With
   
    Menu.Show
    Application.ScreenUpdating = True
End Sub

Merci infiniment Dranreb
 

INFINITY100

XLDnaute Occasionnel
Re : Cherche aide pour simplifier une macro

bonsoir

Encore une modification sur les lignes ci-dessous la macro est un peu plus améliorée

.Range("T2").FormulaR1C1 = "=MAX(Bilan!R12C2,0)"
.Range("U2").FormulaR1C1 = "=MAX(Bilan!R12C4,0)"
.Range("V2").FormulaR1C1 = "=SUM(R2C3,R2C16,R2C11,R2C20)"
.Range("W2").FormulaR1C1 = "=SUM(R2C2,R2C12,R2C8,R2C21)"

ce qui nous donnera en final ceci :

Code:
Sub auto_open()

    Application.ScreenUpdating = False
    
    With Sheets("Bilan")
        .Unprotect Password:="123456789"
        .Range("B6").FormulaR1C1 = "=SUM(BD!R[-4]C[6]:R[1048570]C[6])"
        .Range("B8").FormulaR1C1 = "=SUM(encaissement!R[-6]C:R[1048568]C)"
        .Range("B10").FormulaR1C1 = "=SUM(Creances!R[-8]C[3]:R[1048566]C[3])"
        .Range("B12").FormulaR1C1 = _
        "=MAX(SUM(R[-6]C[2]:R[-2]C[2])-SUM(R[-6]C:R[-2]C),0)"
        .Range("D6").FormulaR1C1 = "=SUM(BD!R[-4]C[2]:R[1048570]C[2])"
        .Range("D8").FormulaR1C1 = "=SUM(Charges!R[-6]C:R[1048568]C)"
        .Range("D10").FormulaR1C1 = "=SUM(Dettes!R[-8]C[1]:R[1048566]C[1])"
        .Range("D12").FormulaR1C1 = _
        "=MAX(SUM(R[-6]C[-2]:R[-2]C[-2])-SUM(R[-6]C:R[-2]C),0)"
        .Range("B14").FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
        .Range("D14").FormulaR1C1 = "=SUM(R[-8]C:R[-2]C)"
        .Protect Password:="123456789"
    End With
    
    With Sheets("Divers")
        .Unprotect Password:="123456789"
        .Range("A2").FormulaR1C1 = "=TODAY()"
        .Range("B2").FormulaR1C1 = _
        "=MAX(SUM(BD!R2C6:R1048576C6),0)"
        .Range("C2").FormulaR1C1 = _
        "=MAX(SUM(BD!R2C8:R1048576C8),0)"
        .Range("D2").FormulaR1C1 = "=R2C3-R2C2"
        .Range("E2").FormulaR1C1 = "=IF(R2C2<>0,AVERAGE(BD!R2C10:R1048576C10),0)"
        .Range("F2").FormulaR1C1 = _
        "=SUMIF(Dettes!R2C5:R1048576C5,"">=0"",Dettes!R2C5:R1048576C5)"
        .Range("G2").FormulaR1C1 = _
        "=SUMIF(Dettes!R2C5:R1048576C5,""<=0"",Dettes!R2C5:R1048576C5)"
        .Range("H2").FormulaR1C1 = "=R2C6+R2C7"
        .Range("I2").FormulaR1C1 = _
        "=SUMIF(Creances!R2C5:R1048576C5,"">=0"",Dettes!R2C5:R1048576C5)"
        .Range("J2").FormulaR1C1 = _
        "=SUMIF(Creances!R2C5:R1048576C5,""<=0"",Dettes!R2C5:R1048576C5)"
        .Range("K2").FormulaR1C1 = "=R2C9+R2C10"
        .Range("L2").FormulaR1C1 = _
        "=MAX(SUM(Charges!R2C4:R1048576C4),0)"
        .Range("P2").FormulaR1C1 = _
        "=MAX(SUM(encaissement!R2C2:R1048576C2),0)"
        .Range("S2").FormulaR1C1 = "=R2C3-R2C16"
        .Range("T2").FormulaR1C1 = "=MAX(Bilan!R12C2,0)"
        .Range("U2").FormulaR1C1 = "=MAX(Bilan!R12C4,0)"
        .Range("V2").FormulaR1C1 = "=SUM(R2C3,R2C16,R2C11,R2C20)"
        .Range("W2").FormulaR1C1 = "=SUM(R2C2,R2C12,R2C8,R2C21)"
        .Protect Password:="123456789"
    End With
   
    Menu.Show
    
    Application.ScreenUpdating = True
    
End Sub

Vous voyez Dranreb et dire je je ne cherche pas comprendre lol :)

En final je vous dis merci vous tous pour vos réponses surtout Dranreb et Victor vous m'avez beaucoup aidé
 

Discussions similaires

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 196
Messages
2 086 099
Membres
103 116
dernier inscrit
kutobi87