2013-05-20 93 views
0

如何將分隔符「^」作爲來自導入的多個txt文件的列應用。請幫助。 ..使用分隔符作爲列表格式用於記錄的txt文件

Private Sub CommandButton1_Click() 


Application.ScreenUpdating = False 

Dim oFileDialog As FileDialog 
Dim LoopFolderPath As String 
Dim oFileSystem As FileSystemObject 
Dim oLoopFolder As Folder 
Dim oFilePath As File 
Dim oFile As TextStream 
Dim RowN As Long 
Dim ColN As Long 
Dim iAnswer As Integer 
On Error GoTo ERROR_HANDLER 

Set oFileDialog = Application.FileDialog(msoFileDialogFolderPicker) 

RowN = 1 
ColN = 1 

With oFileDialog 
If .Show Then 
    ActiveSheet.Columns(ColN).Cells.Clear 

    LoopFolderPath = .SelectedItems(1) & "\" 

    Set oFileSystem = CreateObject("Scripting.FileSystemObject") 
    Set oLoopFolder = oFileSystem.GetFolder(LoopFolderPath) 

    For Each oFilePath In oLoopFolder.Files 
     Set oFile = oFileSystem.OpenTextFile(oFilePath) 

     With oFile 

      Do Until .AtEndOfStream 
       ActiveSheet.Cells(RowN, ColN).Value = .ReadLine 

       LoopFolderPath = Space(1) 
       RowN = RowN + 1 

       Loop 

      .Close 
     End With 
    Next oFilePath 
End If 
iAnswer = MsgBox("Your Textfiles have been Inputted.", vbInformation) 
End With 

EXIT_SUB: 
Set oFilePath = Nothing 
Set oLoopFolder = Nothing 
Set oFileSystem = Nothing 
Set oFileDialog = Nothing 

Application.ScreenUpdating = True 

Exit Sub 

ERROR_HANDLER: 
    ' Some code for error handling 
    Err.Clear 
    GoTo EXIT_SUB 

End Sub 

回答

1
ActiveSheet.RangE("X:X").TextToColumns Destination:=Range("X#"), DataType:=xlDelimited, _ 
    TextQualifier:=xlDoubleQuote, Other:=True, OtherChar:="^" 

當然,你將需要更換的X:通過適當的列和X#通過適當的範圍內X。

使用宏記錄器獲取該代碼花了約30秒。

+0

我試過更換X:X和X#的範圍。它只設置爲行分隔符而不是列分隔符。 (答:A和A1)我嘗試了其他範圍,但給了不滿意的結果:( – jinomar25

+0

我不明白你的意思是行分隔符和列分隔符 – ApplePie

+0

我希望文本在1列。列的分隔符「^」,代碼按行分割文本:(只需要A1列將被使用 – jinomar25

相關問題