2013-06-20 15 views
6

マクロレコーダーによって生成されたこのVBAコードは、以下のとおりです。 csvファイルを特定の列設定で現在のExcelシートにインポートします。現在、csvファイルへのパスは "C:\ Users \ myuser \ Desktop \ logexportdata.csv"にハードコードされています。これを変更して、インポートする.csvファイルをユーザーに検索するよう求めるダイアログプロンプトが表示されるようにするにはどうすればよいですか?excel vbaを開いてダイアログを開きます。

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 

答えて

7

これを試してみてください。作品

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

を!完璧、ありがとう! – jstevens13

関連する問題