用多空格在Excel中打开空格分隔的文本文件

我试图导入一个空格分隔的文本文件到Excel中,但麻烦的是,它只有一个不知名的数量的空间instaed只有一个。 我现在想知道是否有人可以帮助我在导入之前或之后快速移除所有空单元格? 大约有100万行和超过100列,包括所有的空行。 这是我的代码到目前为止。

currentFile = ActiveWorkbook.Name fileToOpen = Application.GetOpenFilename("PRN Files (*.prn), *.prn") If fileToOpen <> False Then Sheets("Rawdata").Cells.ClearContents ' clear raw_data worksheet Workbooks.OpenText Filename:=fileToOpen, DataType:=xlDelimited, Space:=True openedFile = ActiveWorkbook.Name Workbooks(openedFile).ActiveSheet.UsedRange.Copy Destination:=Workbooks(currentFile).Worksheets("Rawdata").Range("A1") Workbooks(openedFile).Close Else MsgBox "No data imported" End If 

真诚,最大

这是一个稍微不同的方法:

  • 获取数据
  • 删除所有额外的空间
  • 把所有的数据放在A
  • 将数据parsing为多个列

这里是代码:

 Sub OpenPrnFile() Dim sh As Worksheet, filetoOpen As String Dim i As Long, s As String, wf As WorksheetFunction Set wf = Application.WorksheetFunction Set sh = Sheets("Sheet1") filetoOpen = Application.GetOpenFilename("PRN Files (*.prn), *.prn") If filetoOpen = "" Then Exit Sub Close #1 Open filetoOpen For Input As #1 sh.Select i = 1 Do Until EOF(1) Line Input #1, s Cells(i, 1) = wf.Trim(s) i = i + 1 Loop Close #1 For j = 1 To i ary = Split(Cells(j, 1), " ") For k = LBound(ary) To UBound(ary) Cells(j, k + 1).Value = ary(k) Next k Next j End Sub