我在使用分隔符“^”将文本拆分为列时出现问题。有人能帮我吗?导入多个.txt文件后分隔符为文本列格式
顶部将与上下格式相同的输出格式。
这里的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
这里添加代码代码已经链接到 – SysDragon
。 – jinomar25
这些问题旨在持续帮助未来的其他人。这个链接的代码将来可能无法使用,正如外部人员和外部公司所处理的那样。在这里添加代码,阅读[FAQ](http://stackoverflow.com/faq)和[How to ask](http://stackoverflow.com/questions/how-to-ask),你将减少你的可能性downvotes或关闭的问题。更好的问题带来更好的答案 – SysDragon