2013-05-20 204 views
-2

我在使用分隔符「^」將文本拆分爲列時出現問題。有人能幫我嗎?導入多個.txt文件後分隔符爲文本列格式

enter image description here

頂部將與上下格式相同的輸出格式。

這裏的Excel的VBA代碼:

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 
       ActiveSheet.Range("A:A").TextToColumns _ 
        Destination:=Range("A1"), DataType:=xlDelimited, _ 
        TextQualifier:=xlDoubleQuote, Other:=True, OtherChar:="^" 
       ActiveSheet.UsedRange.Columns.AutoFit 
       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: 

    Err.Clear 
    GoTo EXIT_SUB 

End Sub 
+1

這裏添加代碼代碼已經鏈接到 – SysDragon

+0

。 – jinomar25

+3

這些問題旨在持續幫助未來的其他人。這個鏈接的代碼將來可能無法使用,正如外部人員和外部公司所處理的那樣。在這裏添加代碼,閱讀[FAQ](http://stackoverflow.com/faq)和[How to ask](http://stackoverflow.com/questions/how-to-ask),你將減少你的可能性downvotes或關閉的問題。更好的問題帶來更好的答案 – SysDragon

回答

0

調用每個插入線後的整列TextToColumns可能導致值被覆蓋。在插入所有值後,僅調用TextToColumnsAutoFit

With oFile 
    Do Until .AtEndOfStream 
    ActiveSheet.Cells(RowN, ColN).Value = .ReadLine 
    LoopFolderPath = Space(1) 
    RowN = RowN + 1 
    Loop 
    .Close 
End With 

ActiveSheet.Range("A:A").TextToColumns Destination:=Range("A1") _ 
    , DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, Other:=True _ 
    , OtherChar:="^" 
ActiveSheet.UsedRange.Columns.AutoFit 

要組織在列中的數據,而不是行的,我建議你插入數據行,然後將它們複製到使用Transpose操作的新的工作表:

Sheets.Add After:=Sheets(1) 
Sheets(1).UsedRange.Copy 
Sheets(2).Range("A1").PasteSpecial Paste:=xlPasteValues, Transpose:=True 
+0

該問題尚未解決。該代碼只做了行分隔符而不是列分隔符分割。 。 。 :( – jinomar25

+0

輸出仍然是一樣的...... – jinomar25

+0

輸出仍然是行分隔符相同,輸出必須是一個列分隔符(... https://docs.google.com/file/d/0B_eArBbGfxv4RTl0NTU3b01oX0U/edit?usp = sharing! – jinomar25

相關問題