Besoin d'aide sur VBA

Fermé
Tintin - 17 oct. 2011 à 17:29
 Tintin - 18 oct. 2011 à 09:59
Bonjour,

Je suis débutant de VBA. Et je dois travailler sur un macro comme au-dessous que je comprend rien. Est-ce que quelqu'un peut ajouter des commentaire à celui-ci pour m'aider à comprendre. Merci par avance.

Sub Sub_By_A_NB()


Sheets("RNCalc(All)").Select
Range("A11:E11").Select
Selection.Copy
Sheets("NBCalc(All)").Select
Range("A11").Select
ActiveSheet.Paste
Sheets("RNCalc(All)").Select
Range("A11").Select
Sheets("NBBase(All)").Select
Testcell = Range("A7").Value
If Testcell = Empty Then
Range("A12:E12").Select
Selection.Copy
Range("A7:E7").Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
End If

Sheets("NBCalc(All)").Select

Length = Range("Length1").Value
'Area 1 is everything. Area 3 is the 5 columns of factors

area1 = "A11:EQ" & Length & ""
area2 = "A11:BT" & Length & ""
area3 = "EK12:EO" & Length & ""

Columns("BS:BS").Select
Selection.Copy

stopper1 = ActiveSheet.Name
stopper2 = " "
Do Until stopper2 = stopper1
ActiveSheet.Next.Select
a = Left(ActiveSheet.Name, 2)
b = Right(ActiveSheet.Name, 5)
stopper2 = a & "Calc" & b
Loop

Columns("EP:EP").Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteComments, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Range(area3).Select
Selection.Copy
Range("A12").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

Range(area1).Activate
Selection.Sort Key1:=Range("A12"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

Columns("EP:EP").Select
Selection.Copy

stopper1 = ActiveSheet.Name
stopper2 = " "
Do Until stopper2 = stopper1
ActiveSheet.Previous.Select
a = Left(ActiveSheet.Name, 2)
b = Right(ActiveSheet.Name, 5)
stopper2 = a & "Base" & b
Loop

Columns("BS:BS").Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteComments, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Range("BS12").Select
d = 1
Do Until d = Empty
ActiveCell.Offset(1, 0).Activate
d = ActiveCell.Value
Loop
ActiveCell.Offset(1, 1).Activate
Selection.Copy
ActiveCell.Offset(0, -1).Activate
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

Range(area2).Select
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(7, 8, 9, 10, 11, 12, 13, 14, 19, 20, 21, 22, 43, 44, 45, 46 _
), Replace:=True, PageBreaks:=False, SummaryBelowData:=True


'Offset by 1 at a time to find row containing "Sub-Total" - looping for each sub-category
Range("B12").Select
stop1 = Application.ActiveCell.Value
k = 1
Do Until stop1 = Empty
i = 0


Do Until stop1 = Empty
ActiveCell.Offset(1, 0).Activate
stop1 = Application.ActiveCell.Value
i = i + 1
Loop
ActiveCell.Offset(0, 13).Activate


Formula_NB i, k



ActiveCell.Offset(1, -120).Activate
stop1 = Application.ActiveCell.Value
k = k + 1
Loop


'Offset by 1 at a time to find row containing "Grand Total"
j = 0
Range("A12").Select
stop2 = Application.ActiveCell.Value
Do Until stop2 = Empty
ActiveCell.Offset(1, 0).Activate
stop2 = Application.ActiveCell.Value
j = j + 1
Loop
ActiveCell.Offset(-1, 14).Activate
Formula_All_NB j, k
j = 0

Range("F5").Select
Selection.EntireColumn.Hidden = True
Range("E8").Select
Selection.ColumnWidth = 5

End Sub



1 réponse

Patrice33740 Messages postés 8556 Date d'inscription dimanche 13 juin 2010 Statut Membre Dernière intervention 2 mars 2023 1 776
17 oct. 2011 à 19:26
Bonjour,

1 - Il serait plus facile de commenter ce code en disposant du classeur, cela éviterait d'être obligé de deviner à quoi ces instructions correspondent exactement (utiliser http://cijoint.fr/ ou https://www.cjoint.com/

2 - Ce code est d'une qualité déplorable et ne respecte pas le B.A.BA de la programmation en VBA. Cela ressemble à un patchwork de code généré par l'enregistreur de macro et d'écriture complémentaire : il est gavé d'instructions totalement inutiles.

3 - Il serait plus bénéfique de tout ré-écrire correctement avant de commenter ce code, mais la tâche en vaut-elle le coup ? Il existe d'excellents tutoriels pour apprendre le VBA.
0
Merci bien. Je vais utiliser http://cijoint.fr/ pour le voir.
0