将文本文件导入Excel VBA空行新列

我有txt文件单列中的数据表。 我需要导入到Excel中,并将每个字段分解成空行的新列。 我不确定最好是在导入中执行此操作还是在导入后执行此操作。

Foobar detail1 detail2 val1 val2 val3 val4 randominfo Widget detail1 detail2 val1 val2 val3 val4 randominfo 

您需要逐行读取文件,并在读取空行时移动。

编辑:忘了回答你的问题…只是在导入。 另一种方式使你再次扫描列表,效率不高。

 Dim r As Integer Dim c As Integer ''Initialize r = 2 c = 1 ''I'm assuming you have row headers or something so row starts at two. ''change to 1 if you want the data to be in the first row. Open [your file path here] For Input As #1 Do Until EOF(1) Line Input #1, readLine If readLine = "" Then 'Index over one column 'Start row indexer over c = c + 1 r = 2 Else 'Output "readLine" to the sheet ActiveSheet.Cells(r, c).Value = readLine 'Index down one row r = r + 1 End If Loop Close #1