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
| Option Explicit
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim lastRow As Integer
Dim fList() As Variant
Dim c1, c2, c3, c4 As String
Dim iRow, iList As Integer
Set ws = Worksheets("Feuil2") 'Mettre ici la feuille dans laquelle sont les données
'Recherche de la dernière ligne
lastRow = ws.Range("B65000").End(xlUp).Row
ReDim fList(3, lastRow - 2)
'On insère les valeurs s'il n'y a pas de cellules vides sur la ligne
iList = 0
For iRow = 2 To lastRow
c1 = ws.Cells(iRow, "B").Value
c2 = ws.Cells(iRow, "C").Value
c3 = ws.Cells(iRow, "E").Value
c4 = ws.Cells(iRow, "F").Value
If Trim(c1) <> "" And Trim(c2) <> "" And Trim(c3) <> "" And Trim(c4) <> "" Then
fList(0, iList) = c1
fList(1, iList) = c2
fList(2, iList) = c3
fList(3, iList) = c4
iList = iList + 1
End If
Next iRow
'A la fin, seulement iList lignes vraiment insérées, et il faut retourner le tableau
Dim fList2() As Variant
ReDim fList2(iList - 1, 3)
Dim i, j As Integer
For i = 0 To iList - 1
For j = 0 To 3
fList2(i, j) = fList(j, i)
Next j
Next i
'On affecte ce tableau au listView
lstContrats.List = fList2
End Sub |
Partager