Choix d'un fichier

Résolu/Fermé
ghislaineg Messages postés 1 Date d'inscription mardi 14 juin 2011 Statut Membre Dernière intervention 9 septembre 2011 - Modifié par ghislaineg le 9/09/2011 à 20:47
 ghislaineg - 12 sept. 2011 à 14:59
Bonjour,
j'ai une macro qui va chercher un fichier .csv mais j'aimerais que l'utilisateur choisisse sont fichier et par la suite l'importe..
voici ma macro de base:


Sub fiche()
'
' fiche Macro
'
' Touche de raccourci du clavier: Ctrl+y

Range("a1:e800").Select
Selection.ClearContents
rep = InputBox("nom du fichier")
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\Administrateur\Bureau\" & rep & ".csv", _
Destination:=Range("$A$1"))
.Name = "& rep"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Columns("A:E").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Range("A1:D2").Select
'ActiveWorkbook.Names.Add Name:="ente", RefersToR1C1:="=Feuil1!R1C1:R2C4"
'ActiveWorkbook.Names("ente").Comment = ""
With ActiveSheet.PageSetup
'en-tête de page
.LeftHeader = Sheets(1).Range("a1") '<-- contenu de la cellule A1 (sans précision = style par défaut)
.CenterHeader = "&G&12&""Comic Sans Ms""Sommaire de Pesée" '<-- texte (style gras + taille 12 + style police)
.RightHeader = Sheets(1).Range("d1") '<-- texte + indice
'pied de page
.LeftFooter = "&I&D / &T" '<-- date / heure (style italique)
.CenterFooter = "&G&A" & Chr(10) & "&G&F" '<-- nom feuille (activation style gras) + saut de ligne + nom fichier (désactivation style gras)
.RightFooter = "&8&P/&N" '<-- numéro de page / nombre de pages (taille 8)
End With


End Sub

Je sais que c'est cette section qur je dois changer :rep = InputBox("nom du fichier")
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\Administrateur\Bureau\" & rep & ".csv", _
Destination:=Range("$A$1"))
.Name = "& rep"
pouvez-vous m'aider..

Salutation Ghislaine


End Sub


A voir également:

1 réponse

gbinforme Messages postés 14946 Date d'inscription lundi 18 octobre 2004 Statut Contributeur Dernière intervention 24 juin 2020 4 701
9 sept. 2011 à 23:14
bonjour

Comme ceci cela devrait fonctionner :

...
Selection.ClearContents
ChDir ("C:\Documents and Settings\Administrateur\Bureau\")
rep = Application.GetOpenFilename("Text Files (*.csv), *.csv")
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & rep, _
Destination:=Range("$A$1"))
.....
1
Bonjour,
merci de ton aide cela fonctionne tres bien .:)

Salutation Ghislaine
0