Problème de chemin lors d'un import de données

Chico64

XLDnaute Junior
Bonjour A Vous,

Le code ci-dessous permet d'importer des données dans une feuille excel, pour cet exemple les données se trouvent dans un fichier .txt qui se nomme ici TEST_kpi_2.txt.

Ce code marche très bien.

Code:
Sub importDonnees()

Sheets("DonneesImportees").Select

        
        With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;[COLOR="Red"]C:\Documents and Settings\toto titi\Desktop\test\Macro\TEST_kpi_2.txt[/COLOR]" _
        , Destination:=Range("A1"))        

        .Name = "TEST_kpi_2"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    End Sub

Je souhaiterais à la place d'écrire le chemin en dur (comme rouge au dessus), mettre une variable, ici la variable chemin (voir en rouge en dessous).
Comme ça si l'utilisateur change de place le dossier contenant le fichier excel et le fichier txt. il n'y aura pas d'erreur.

Ma solution qui ne marche pas très bien :

Code:
Chemin = ActiveWorkbook.Path & "\TEST_kpi_2.txt"


A la place de :
Code:
        With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;[COLOR="Red"]C:\Documents and Settings\toto titi\Desktop\test\Macro\TEST_kpi_2.txt[/COLOR]" _
        , Destination:=Range("A1"))

J'ai écris ça:
Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;[COLOR="red"]chemin[/COLOR]" _
        , Destination:=Range("A1"))

Mais malheureusement quand je débeuge mon nouveau code il y a une erreur à ce niveau

Code:
.Refresh BackgroundQuery:=False

Mais peut etre qu'il y a une erreur dans :

Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;[COLOR="red"]chemin[/COLOR]" _
        , Destination:=Range("A1"))


Je vous remercie de votre aide.

Chico
 

Discussions similaires

Réponses
4
Affichages
569

Statistiques des forums

Discussions
312 610
Messages
2 090 217
Membres
104 452
dernier inscrit
hamzamounir