2017-11-07 1 views
2

텍스트 파일을 열고 읽는 Excel VBA에 코드가 있지만 파일의 내용을 복사하지만 구분 기호와 함께 붙여 넣지는 않습니다.텍스트 파일을 열고 읽은 다음 구분 기호가있는 파일의 내용을 복사합니다.

열려있는 파일의 내용을 복사하여 다른 통합 문서에 붙여 넣으 려합니다. 다음은

당신은 당신이처럼 구분 된 데이터를 가져 순간 내가 대신 개방 & 복사/붙여 넣기 내용의

Sub ImportTextFile() 

Dim SheetName As String 
Dim TMPWorkBook As Workbook 
Dim FilePath As String 
Dim TxtFilePath As String 
Dim TxtFileName As String 

Set WB = ThisWorkbook 

path = "C:\Users\" 

SheetName = "Test_Result" 
TxtFileName = path & "244.txt" 

Workbooks.OpenText Filename:= _ 
    TxtFileName _ 
    , Origin:=437 


Set TMPWorkBook = ActiveWorkbook 
TMPWorkBook.Sheets(1).Select 
Cells.Select 
Selection.Copy 

'ResultWB.Activate 
Windows("Sample.xlsb").Activate 

'ResultWB.Sheets(SheetName).Select 
Sheets("Data").Select 
Range("A1").Select 
ActiveSheet.Paste 
Application.CutCopyMode = False 
Cells.Select 
Cells.EntireColumn.AutoFit 
ActiveSheet.Range("A1").Select 
TMPWorkBook.Close savechanges:=False 

Image of result

enter image description here

+0

구분 기호 텍스트 파일에는 어떤 문자가 들어 있습니까? 쉼표 또는 다른 것? 그리고 당신이 얻는 결과는 무엇입니까? – PankajKushwaha

+0

샘플이 아래에 있으면 delimeter가 없습니다. 00001105656 240112 000000000DMO SPA 237679 1 620258901AFACHAT FACTURE 2017030720170719ACHATACK 0 00000317441400000014284000000000000000000 – Wylie

+0

구분 기호가 없으면 데이터가 어떻게 분석 될 것입니까? Excel을 나가십시오. 데이터를 어떻게 파싱합니까? 엑셀이 어디에서 선을 깰지 모르기 때문에 불가능합니다. – PankajKushwaha

답변

0

내가 제안에있는 코드입니다 메뉴 옵션 "Data => From Text"를 통해. 게시 한 샘플에 따르면 스페이스를 구분 기호로 사용할 수 있습니다. 시작해야 할 코드는 다음과 같습니다.

Dim wb As Workbook, sh As Worksheet 
Dim path As String 
Dim qt As QueryTable 

path = "M:\SSC TQM\Projects\_CC\BPLG\ETAPAI_20170724.txt" 
Set wb = Workbooks("TEMPLATE BPLG.xlsb") 
Set sh = wb.Worksheets("Data") 

'Clear existing Query Tables 

For Each qt In sh.QueryTables 
    qt.Delete 
Next qt 


With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;" & path, Destination:=Range(_ 
    "$A$1")) 
    .Name = "Data" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 850 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = False 
    .TextFileSpaceDelimiter = True 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 
sh.QueryTables(1).Delete 
+0

저에게 효과가있는 고마워요. – Wylie

관련 문제