Excel 使用分隔符作为导入的txt文件的列格式
如何将分隔符“^”应用为导入的多个txt文件中的列。请帮忙Excel 使用分隔符作为导入的txt文件的列格式,excel,vba,Excel,Vba,如何将分隔符“^”应用为导入的多个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
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
当然,您需要将X:X替换为适当的列,将X#替换为适当的范围
使用宏记录器获取代码大约需要30秒。我尝试替换X:X和X#的范围。它只设置为行分隔符而不是列分隔符。(A:A和A1)我尝试了其他范围,但给出了不满意的结果:(我不理解行分隔符和列分隔符的含义。我希望文本位于1列中。文本由分隔符“^”拆分为列,代码按行拆分文本:(.只使用A1列。你所说的对我来说毫无意义。发布你拥有的和想要的屏幕截图。导入txt文件后,我希望顶部格式与底部格式相同…:)
ActiveSheet.RangE("X:X").TextToColumns Destination:=Range("X#"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, Other:=True, OtherChar:="^"