Bonjour à tous,
Pour réaliser une requête SQL avec une jointure sur 2 onglets dont le nom est avec des espaces, je partage ma solution
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Dim oCnx As New ADODB.Connection Dim oRSet As ADODB.Recordset Dim sSql As String '--- Range for SQL Tables --- ThisWorkbook.Names.Add Name:="EnCours", RefersTo:=worksheets("Données en cours").Range("h8:i65000") '-> Solution propre, calculer la dernière ligne non vide ThisWorkbook.Names.Add Name:="Mailing", RefersTo:=Worksheets("Mailing List").Range("a1:d65000") ' -> Cf. commentaire ci-dessus '---Connecting to the Data Source--- With oCnx .Provider = "Microsoft.ACE.OLEDB.12.0" .ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=Yes"";" .Open End With '---Run the SQL SELECT Query--- sSql = "SELECT DISTINCT [Responsable], Prenom, Name, Email FROM EnCours AS R " & _ " INNER JOIN Mailing AS M ON [R].[Responsable]= [M].[Name] " & _ " WHERE [Responsable] <>'' " & _ " ORDER BY [Responsable] ASC " Set oRSet = oCnx.Execute(sSql) ThisWorkbook.Names.Item("EnCours").Delete ThisWorkbook.Names.Item("Mailing").Delete Do While Not oRSet.EOF Debug.Print oRSet![Prenom] & " - " & oRSet![Name] & " - " & oRSet![Email] oRSet.Movenext Loop '---Clean up--- oRSet.Close oCnx.Close Set oCnx = Nothing Set oRSet = Nothing
Partager