拆分文本到行excel
我有一个VBA代码,我将一个txt文件导入到一个单元格中。 这是代码(这不是那么重要):
Sub ReadFile() ' Requires a reference to Microsoft Scripting Runtime (Tools > References) Dim FSO As FileSystemObject Dim FSOFile As File Dim FSOStream As TextStream Dim Rand Dim ws As Worksheet Set ws = Worksheets("Sheet1") Set FSO = New FileSystemObject Set FSOFile = FSO.GetFile("C:\Users\sdagfsgedg\Desktop\20121122.log") Set FSOStream = FSOFile.OpenAsTextStream(ForReading, TristateUseDefault) Rand = 1 Do While Not FSOStream.AtEndOfStream ws.Cells(Rand, 1).Value = FSOStream.ReadAll Loop End Sub
文本文件20121122.log约有20.000行,全部导入到一个单元格中。 我怎样才能把这个单元格分成20.000个单元格(如果日志有20,000个单元格)。 我不想逐行读取文本文件…我想读取所有(它的方式更快),然后将每一行分开一行。
后来编辑:或者,如果有另一种解决scheme来读取日志文件,并将文本作为行来粘贴(不是像我现在这样在一个单元格中的所有内容)
//代码未经testing
Sub ReadFile() Dim FSO As FileSystemObject Dim FSOFile As File Dim FSOStream As TextStream Dim Rand Dim row Dim ws As Worksheet Set ws = Worksheets("Sheet1") Set FSO = New FileSystemObject Set FSOFile = FSO.GetFile("C:\Users\sdagfsgedg\Desktop\20121122.log") Set FSOStream = FSOFile.OpenAsTextStream(ForReading, TristateUseDefault) Rand = 1 Dim content As String Dim lines As Variant Dim intIndex As Integer content = FSOStream.ReadAll lines = split(content, Chr(10)) For intIndex = LBound(lines) To UBound(lines) ws.Cells(Rand, 1).Value = lines(intIndex) Rand = Rand + 1 Next End Sub