Bonjour,
Je dois importer un fichier csv, pour ce faire, j'ai fait ce petit script qui fonctionne bien. Mon problème est que le nom de fichier est en dur dans le code. Je souhaite pouvoir le choisir. J'ai donc mis en variable le nom de fichier avec:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
  Dim MonFichier
  MonFichier = Application.GetOpenFilename()
Et remplacer le nom réel du fichier par MonFichier. Malheureusement cela ne fonctionne pas. Cela bloque sur With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & MonFichier, Destination:=Range("BASE!$A$1"))

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
40
41
42
43
44
45
46
47
48
49
 
Sub import()
'
' Copie fichier CSV 322976_animateurs_Service_regional.csv
'
 
'
Sub import()
'
' Copie fichier CSV 322976_animateurs_Service_regional.csv
'
 
'
 Dim MonFichier
  MonFichier = Application.GetOpenFilename()
 
 
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & MonFichier, Destination:=Range("BASE!$A$1"))
'
'''     With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Users\PASCAL\Desktop\322976_animateurs_Service_regional.csv", Destination:=Range("BASE!$A$1"))
'
       .Name = "DonnéesExternes_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "\"
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
Auriez vous une idée, merci pour votre aide.