Option Explicit
Private Sub CheckBox1_Click()
With Sheets("Feuil1")
If .AutoFilterMode = True Then .AutoFilterMode = False
If Me.CheckBox1 Then
Me.CheckBox1.Caption = "Ville"
Label_ville_cp1 = "Ville :"
tb1 = .Range("A2:A" & derlg)
ComboBox_ville_cp1.List = tb1
Label_ville_cp2 = "Code Postal :"
Set TbCp = Nothing
cp_doublon
ComboBox_ville_cp2.List = tablo
Label2 = "Recherche par Ville :"
Else
Me.CheckBox1.Caption = "CP"
Label_ville_cp1 = "Code Postal :"
Set TbCp = Nothing
cp_doublon
ComboBox_ville_cp1.List = tablo
tb1 = .Range("A2:A" & derlg)
Label_ville_cp2 = "Ville :"
ComboBox_ville_cp2.List = tb1
Label2 = "Recherche par Code Postal :"
End If
ComboBox_ville_cp1.ListIndex = -1
ComboBox_ville_cp2.ListIndex = -1
TextBox_dept = ""
TextBox_Region = ""
TextBox_Sp = ""
TextBox_Prefect = ""
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
End With
End Sub
Private Sub CmdAnnuler_Click()
Unload Me
End Sub
Private Sub ComboBox_ville_cp1_Change()
Application.ScreenUpdating = False
Dim verif As Boolean
ComboBox_ville_cp2.Clear
With Sheets("Feuil1")
If CheckBox1 Then
.Range("$A$1:$I$1").AutoFilter
.Range("$A$1:$I$" & derlg).AutoFilter Field:=1, Criteria1:=ComboBox_ville_cp1
tb2 = .Range("A2:I" & derlg).SpecialCells(xlCellTypeVisible)
ComboBox_ville_cp2.AddItem tb2(1, 2)
TextBox_dept = tb2(1, 3)
TextBox_Region = tb2(1, 4)
TextBox_Sp = tb2(1, 6)
TextBox_Prefect = tb2(1, 5)
TextBox1 = tb2(1, 7)
TextBox2 = tb2(1, 8)
TextBox3 = tb2(1, 9)
.AutoFilterMode = False
Else
If Len(ComboBox_ville_cp1) = 5 Then
.Range("$A$1:$I$1").AutoFilter
.Range("$A$1:$I$" & derlg).AutoFilter Field:=2, Criteria1:=ComboBox_ville_cp1
tb2 = .Range("A2:I" & derlg).SpecialCells(xlCellTypeVisible)
For x = 1 To UBound(tb2, 1)
ComboBox_ville_cp2.AddItem tb2(x, 1)
Next x
TextBox_dept = tb2(1, 3)
TextBox_Region = tb2(1, 4)
TextBox_Sp = tb2(1, 6)
TextBox_Prefect = tb2(1, 5)
ComboBox_ville_cp2.ListIndex = 0
TextBox1 = tb2(1, 7)
TextBox2 = tb2(1, 8)
TextBox3 = tb2(1, 9)
.AutoFilterMode = False
End If
End If
If Len(ComboBox_ville_cp1) = 5 Then
verif = False
For y = 1 To UBound(tb2, 1)
If Str(tb2(y, 2)) = Str(ComboBox_ville_cp1) Then
verif = True: Exit For
End If
Next y
If verif = False Then
MsgBox "aucune correspondance trouvée"
ComboBox_ville_cp2.Value = "INCONNU"
ComboBox_ville_cp2.Clear
TextBox_dept = ""
TextBox_Region = ""
TextBox_Sp = ""
TextBox_Prefect = ""
End If
End If
End With
Application.ScreenUpdating = True
End Sub
Private Sub UserForm_Activate()
Set TbCp = Nothing
With Sheets("Feuil1")
If .AutoFilterMode = True Then .AutoFilterMode = False
derlg = .Range("A" & .Rows.Count).End(xlUp).Row
Me.CheckBox1.Caption = "CP"
Label_ville_cp1 = "Code Postal :"
cp_doublon
ComboBox_ville_cp1.List = tablo
tb1 = .Range("A2:A" & derlg)
Label_ville_cp2 = "Ville :"
ComboBox_ville_cp2.List = tb1
End With
End Sub