2013-06-20 16 views
6

Ho questo codice vba sotto che è stato generato dal registratore macro. Importa un file csv nel foglio excel corrente con alcune impostazioni specifiche della colonna. In questo momento il percorso del file csv è codificato in "C: \ Users \ myuser \ Desktop \ logexportdata.csv". Come posso cambiare questo in modo che ci sia una finestra di dialogo che chiede all'utente di trovare il file .csv da importare?excel vba apri finestra di dialogo per importare csv

Sub Import_log() 

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;C:\Users\myuser\Desktop\logexportdata.csv", Destination:=Range(_ 
    "$A$2")) 
    .Name = "logexportdata" 
    .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 = 2 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(5, 2, 2, 2, 2, 2, 9, 9, 9, 9, 9) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 
End Sub 

risposta

7

provare questo:

Sub Import_log() 

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;" & getFile, Destination:=Range(_ 
    "$A$2")) 
    .Name = "logexportdata" 
    .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 = 2 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(5, 2, 2, 2, 2, 2, 9, 9, 9, 9, 9) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 
End Sub 


Function GetFile() As String 
Dim filename__path As Variant 
filename__path = Application.GetOpenFilename(FileFilter:="Csv (*.CSV), *.CSV", Title:="Select File To Be Opened") 
If filename__path = False Then Exit Function 
GetFile = filename__path 
End Function 
+0

che funziona! Perfetto, grazie! – jstevens13

Problemi correlati