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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| Option Explicit
Sub traitement()
'Déclaration des variables
Dim oRng As Range
Dim t As Integer
Dim ListeLig As String
Dim LigChoisie As String
Dim i, j As Integer
Dim oTable() As Double, x As Integer
Dim oRnd As Integer, oTrouve As Integer
'On définie oRng comme l'ensemble des cellules utilisées de la feuille TCD
Set oRng = Worksheets("TCD").UsedRange
'Avec "échantillon"
With Worksheets("Échantillon")
'On recopie les valeurs de oRng en A1
.Range("A1").Resize(oRng.Rows.Count, oRng.Columns.Count).Value = oRng.Value
'On redéfinie oRng sur F1 de "échantillon"
Set oRng = .Range("F1")
'On boucle de i = dernière ligne non vide de la colonne 7 à 1 (décroissant)
For i = .Cells(.Cells.Rows.Count, 6).End(xlUp).Row - 1 To 1 Step -1
'Si on a une valeur numérique en oRng avec un décalage de i lignes...
If IsNumeric(oRng.Offset(i, 0)) Then
'... alors sur la colonne à droite on place sa valeur absolue
oRng.Offset(i, 1) = Abs(oRng.Offset(i, 0))
'Si cette valeur absolue est < à 150000
If oRng.Offset(i, 1) < 150000 Then
'On supprime la ligne
oRng.Offset(i, 1).EntireRow.Delete
End If
End If
Next i
'le code qui permet de remplir les cellules vides
Set oRng = .Range("A3")
For i = 0 To .Cells(.Cells.Rows.Count, 1).End(xlUp).Row - 1
For j = 0 To 4
If oRng.Offset(i, j) = "" Then
oRng.Offset(i, j).Value = oRng.Offset(i - 1, j)
End If
Next j
Next i
'On redéfinie oRng sur la cellule G1 (toujours de "Échantillon")
Set oRng = .Range("G3")
'On définie x = 1
x = 1
'On boucle de i = 1 à la dernière ligne non vide de la colonne 7
For i = 1 To .Cells(.Cells.Rows.Count, 7).End(xlUp).Row - 1
'Si oRng avec un décalage de i ligne est différent de "rien"
If oRng.Offset(i, 0) <> "" Then
'Alors on refédinie le tableau oTable
ReDim Preserve oTable(1 To 2, 1 To x)
'On lui ajoute la valeur
oTable(1, x) = oRng.Offset(i, 0)
'Et la ligne
oTable(2, x) = oRng.Offset(i, 0).Row
'Et on agrandi x (ou agrandir la tableau, par la suite)
x = x + 1
End If
Next i
'Si le nombre de valeur de oTable est suppérieur à 6
If UBound(oTable, 2) >= 6 Then
With ThisWorkbook.Worksheets("Échantillon")
ListeLig = "" ' initialisation de la liste des lignes choisies pour cette feuille
For i = 1 To 6 ' on va piocher trois lignes
' définition de la ligne piochées
LigChoisie = Int((.Cells(.Rows.Count, 1).End(xlUp).Row - 3) * Rnd + 3)
' tant que la ligne piochée a déjà été utilisée
While ListeLig Like "*$" & LigChoisie & "$*"
' on en pioche une autre
LigChoisie = Int((.Cells(.Rows.Count, 1).End(xlUp).Row - 3) * Rnd + 3)
Wend
' on ajoute la ligne piochée à la liste des lignes utilisées
ListeLig = ListeLig & "$" & LigChoisie & "$"
' on écrit la ligne
.Cells(LigChoisie, 1).Resize(1, .UsedRange.Columns.Count).Copy ThisWorkbook.Worksheets("CPN1").Cells(2, 1).Offset(t, 0)
t = t + 1
Next i
End With
Else
MsgBox "Pas 6 lignes différentes dans le tableau."
End If
End With
End Sub |
Partager