2013-06-20 2 views
6

매크로 레코더에서 생성 한이 vba 코드가 아래에 있습니다. csv 파일을 특정 Excel 설정으로 현재 Excel 시트로 가져옵니다. 현재 csv 파일의 경로는 "C : \ Users \ myuser \ Desktop \ logexportdata.csv"로 하드 코딩되어 있습니다. 가져올 CSV 파일을 찾도록 묻는 대화 상자가 표시되도록하려면 어떻게 변경해야합니까?엑셀 vba 열기 대화 상자에서 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 

답변

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

관련 문제