Optimisation d'un code

Bonjour,

Est-ce que c'est possible d'optimiser ce code placé dans une feuille?

Les listbox sont des listbox activeX et sont dans la feuille M.

Merci pour les conseils:

Sub AjoutAuxListbox()

Set GS = Sheets("Gestion du Stock")
Set M = Sheets("Magasin")

'------------------ Listbox1 --------------
Ln = GS.Range("H" & Rows.Count).End(xlUp).Row 'trouve la dernière ligne
ListBox1.Clear
With Sheets("Magasin").ListBox1
    .Top = 354
    .Left = 204
    .Width = 150
    .Height = 78
End With
For i = 2 To Ln
        ListBox1.AddItem
        ListBox1.List(i - 2, 0) = GS.Cells(i, 8)
        ListBox1.List(i - 2, 1) = GS.Cells(i, 9)
Next i
'------------------ Listbox2 --------------
Ln = GS.Range("J" & Rows.Count).End(xlUp).Row 'trouve la dernière ligne
ListBox2.Clear
With Sheets("Magasin").ListBox2
    .Top = 354
    .Left = 388
    .Width = 150
    .Height = 78
End With

For i = 2 To Ln
        ListBox2.AddItem
        ListBox2.List(i - 2, 0) = GS.Cells(i, 10)
        ListBox2.List(i - 2, 1) = GS.Cells(i, 11)
Next i

'------------------ Listbox3 --------------
Ln = GS.Range("L" & Rows.Count).End(xlUp).Row 'trouve la dernière ligne
ListBox3.Clear
With Sheets("Magasin").ListBox3
    .Top = 354
    .Left = 570
    .Width = 150
    .Height = 78
End With
For i = 2 To Ln
        ListBox3.AddItem
        ListBox3.List(i - 2, 0) = GS.Cells(i, 12)
        ListBox3.List(i - 2, 1) = GS.Cells(i, 13)
Next i
End Sub

Pourquoi simplifier ? c'est clair et lisible.

Tout au plus, pour ne pas répéter le code tu peux l'encapsuler dans un programme

Quelque chose du genre ...

Sub MAJ(malistebox, aa, bb, ln)
malistebox.Clear
With malistebox
    .Top = 354
    .Left = 570
    .Width = 150
    .Height = 78
End With
For i = 2 To Ln
        malistebox.AddItem
        malistebox.List(i - 2, 0) = GS.Cells(i, aa)
        malistebox.List(i - 2, 1) = GS.Cells(i, 1bb)
Next i
End Sub

Bonjour Steelson,

Merci, je vais laisser comme ça alors.

ddetp88

Rechercher des sujets similaires à "optimisation code"