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
| 'Dans Feuil1 (Input)
Private Sub Worksheet_Change(ByVal target As Range)
'Action only if the cell containing the data validation is triggered
If target.Row = 2 And target.Column = 1 Then
Update_based_on_input target
End If
End Sub 'Worksheet_Change
Private Sub Update_based_on_input(ByVal cell As Range)
'City list
Const nb_cities = 5
Dim cities(0 To nb_cities) As String
cities(0) = "Marseille"
cities(1) = "Meaux"
cities(2) = "Miramas"
cities(3) = "Paris"
cities(4) = "Pau"
cities(5) = "Troyes"
'Search cities in list which match the start of the input
Dim filtered_list() As String
current_input = cell.Value
nb_matches = 0
For i = 0 To nb_cities
element = cities(i)
search_match = InStr(1, element, current_input, vbTextCompare)
If search_match = 1 Then
nb_matches = nb_matches + 1
ReDim Preserve filtered_list(0 To nb_matches - 1)
filtered_list(nb_matches - 1) = element
End If
Next i
'Write the city list on the range for validation
For i = 0 To nb_matches - 1
Worksheets("Aux").Cells(2, i + 1).Value = filtered_list(i)
Next i
'Erase the other cells
For i = nb_matches To nb_cities
Worksheets("Aux").Cells(2, i + 1).Value = ""
Next i
End Sub 'Update_based_on_input |
Partager