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 96 97 98 99 100 101 102
|
Option Explicit
' La DLL Excel doit être cochée
Public ListeCle As Variant, ListeElement As Variant
Sub ChargerLaCombobox1()
Dim J As Long, DerniereLigne As Long
Dim Chemin As String
Dim xlApp As Excel.Application 'Application Excel pour ouvrir un fichier XLS
Dim xlWb As Excel.Workbook 'Classeur Excel
Dim xlWs As Excel.Worksheet 'Feuille du classeur
On Error GoTo Fin:
Chemin = ActiveDocument.Path & "\Tadresses.xlsx" ' "\test.xlsx"
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Open(Chemin) 'Ouverture du fichier
Set xlWs = xlWb.Worksheets(1) 'Utilisation de la première feuille
With xlWs
DerniereLigne = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
If DerniereLigne <= 2 Then GoTo Fin
ChargerEtTrierLaListe .Range("A2:A" & DerniereLigne)
End With
'Récupère les données triées de la matrice ListeCle
With ThisDocument
.ComboBox1.Clear
For J = LBound(ListeCle) To UBound(ListeCle)
.ComboBox1.AddItem ListeCle(J)
Next J
.ComboBox1.ListIndex = 0
End With
Fin:
xlWb.Close savechanges:=False
xlApp.Quit
Set xlWs = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub
Sub ChargerEtTrierLaListe(ByVal AireCombo As Excel.Range)
Dim CelluleCombo As Excel.Range
Dim CtrI As Integer, CtrJ As Integer
Dim Tempo1, Tempo2
Dim MaListe As Object
On Error GoTo FinDico
Set MaListe = CreateObject("Scripting.Dictionary")
' Ajout des différents enregistrements sans doublons dans le Dico
'-----------------------------------------------------------------
For Each CelluleCombo In AireCombo
If Trim(CelluleCombo.Value) <> "" Then
If Not MaListe.Exists(CelluleCombo.Value) Then
MaListe.Add (CelluleCombo.Value), CStr(CelluleCombo.Value)
End If
End If
Next CelluleCombo
ListeCle = MaListe.Keys
ListeElement = MaListe.Items
' Tri par ordre alphabétique
'----------------------------
For CtrI = 0 To MaListe.Count - 2
For CtrJ = CtrI + 1 To MaListe.Count - 1
If ListeElement(CtrI) > ListeElement(CtrJ) Then
Tempo1 = ListeCle(CtrJ)
Tempo2 = ListeElement(CtrJ)
ListeElement(CtrJ) = ListeElement(CtrI)
ListeCle(CtrJ) = ListeCle(CtrI)
ListeCle(CtrI) = Tempo1
ListeElement(CtrI) = Tempo2
End If
Next CtrJ
Next CtrI
GoTo FinDico
FinDico:
Set MaListe = Nothing
End Sub |
Partager