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
| Sub TestProcedure()
Dim WS As Worksheet
Dim i As Long
Dim mt() As String
Dim a As Long
Dim X1 As Long, X2 As Long
Dim rng As Range
Dim Valch As Range
Set WS = ThisWorkbook.Worksheets("Feuil2")
With WS
'Definir la zone de recherche (1ere ligne de la feuille ici)
Set rng = .Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
'Recherche de la colonne âge
Set Valch = rng.Find(What:="âge", lookat:=xlWhole, searchorder:=xlByColumns)
If Valch Is Nothing Then
MsgBox "Pas de correspondance"
Exit Sub
Else
X1 = Valch.Column
End If
Set Valch = Nothing
'Recherche de la colonne nom
Set Valch = rng.Find(What:="nom", lookat:=xlWhole, searchorder:=xlByColumns)
If Valch Is Nothing Then
MsgBox "Pas de correspondance"
Exit Sub
Else
X2 = Valch.Column
End If
For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
If .Cells(i, X1) = 15 Then
a = a + 1
'Toutes les réponses sont stockées dans une variable
ReDim Preserve mt(1 To a)
mt(a) = .Cells(i, X2)
End If
Next i
End With
End Sub
Sub Test2()
Dim WS As Worksheet
Dim c As Range
Dim rng As Range
Dim a As Long
Dim mt() As String
Dim Valch As Range
Set WS = ThisWorkbook.Worksheets("Feuil2")
With WS
'Definir la zone de recherche (1ere ligne de la feuille ici)
Set rng = .Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
'Recherche de la colonne âge
Set Valch = rng.Find(What:="âge", lookat:=xlWhole, searchorder:=xlByColumns)
If Valch Is Nothing Then
MsgBox "Pas de correspondance"
Exit Sub
Else
X1 = Valch.Column
End If
Set Valch = Nothing
'Recherche de la colonne nom
Set Valch = rng.Find(What:="nom", lookat:=xlWhole, searchorder:=xlByColumns)
If Valch Is Nothing Then
MsgBox "Pas de correspondance"
Exit Sub
Else
X2 = Valch.Column
End If
Set rng = .Range(.Cells(2, X1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, X1))
For Each c In rng
If c.Value = 15 Then
a = a + 1
ReDim Preserve mt(1 To a)
mt(a) = c.Offset(, X2 - X1)
End If
Next c
End With
End Sub |
Partager