2013-10-10 2 views
0

텍스트 파일을 가져 와서 "|"로 구분할 수있는 매크로를 기록했습니다. 그러나이 매크로는 하루 동안 여러 번 실행해야합니다. 가져 오는 텍스트 파일의 끝에 변경되는 타임 스탬프가 있습니다. 셀 디렉터리 및 현재 시간을 기반으로 변경 내 vba 코드로 셀을 참조하고 싶습니다.셀 값을 사용하여 VBA를 통한 텍스트 가져 오기 디렉토리 결정

Sub DataImport() 
Dim LResult As String 

LResult = Dir(ActiveSheet.Range("C6").Select) 

ActiveSheet.Range("B8:G2000").Cells.ClearContents 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;LResult", Destination:= _ 
     Range("$B$8")) 
     .Name = "CustomerEngagement_1" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 65001 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = False 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileOtherDelimiter = "|" 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
    End With 
End Sub 

상단 부분은 나 ...

생각을 LRESULT에 셀 디렉토리를 만들려고 노력하고 수입을 참조? 나는 그것을 사용할 수 있는지 FileSystemOperator를 조사하고있다.

+1

타임 스탬프의인가 파일 이름? 예를 보여줄 수 있다면 도움이 될 것입니다. –

답변

0

Dir()은 파일 이름 만 반환하므로 파일을 QueryTable 만들기에 전달하려면 디렉토리 경로를 추가해야합니다. 이 질문은 여전히 ​​관련이있는 경우 (테스트되지 않은)이 같은

뭔가

Sub DataImport() 

Const FOLDER_PATH as string = "C:\data\example\" 
Dim LResult As String 

'check for a .txt file with a name containing the value from C6 
LResult = Dir(FOLDER_PATH & "*" & ActiveSheet.Range("C6").Value & "*.txt") 

if Len(LResult)=0 Then 
    msgbox "No matching file located" 
    Exit sub 
end if 

ActiveSheet.Range("B8:G2000").Cells.ClearContents 
With ActiveSheet.QueryTables.Add(Connection:= "TEXT;" & FOLDER_PATH & LResult, _ 
            Destination:= Range("$B$8")) 
'... 


End Sub 
0

그나마 알고 있지만 해결책은 다음과 같습니다

LResult = ActiveSheet.Range("C6").Value 

그리고 :

"TEXT;" & LResult, Destination:= _ 
관련 문제