此刻我遇到了这个问题...我有以下文本文件格式(来自电子邮件,应该每周自动读入excel)。:
TextTextText
Text
TextText
TextTextText
2/2 2/3 2/4 2/5 2/6
63 61 65 67 62
59 51 45 11 20
11 22 41 32 55
TextTextText
Text
TextText
TextTextText
我基本上只需要数据的最后一列。所以数据为2/6。现在我该如何提取?拆分对数字行不起作用,因为它们之间有不同的空格(前2个之间有2个,之后有3个)。开头的文字可以有不同的长度。但总有! 35个数据行。
我的问题基本上是: 1.您如何识别它是否是数据行? 2.您如何正确分割数据线?
这是我到目前为止所得到的......
Sub ReadFile()
Dim FSO As FileSystemObject
Dim FSOFile As File
Dim FSOStream As TextStream
Dim aintData(35, 1) As Integer
Set FSO = New FileSystemObject
Set FSOFile = FSO.GetFile("C:\users\mp\Desktop\Test.txt")
Set FSOStream = FSOFile.OpenAsTextStream(ForReading, TristateUseDefault)
Do While Not FSOStream.AtEndOfStream
Debug.Print FSOStream.ReadLine
Loop
End Sub
答案 0 :(得分:0)
尝试一下:
Sub tgr()
Dim oFSO As Object
Dim varText As Variant
Dim arrText() As String
Dim strLine As String
Dim strResult As String
Dim strFilePath As String
strFilePath = Application.GetOpenFilename("Text Files, *.txt")
If strFilePath = "False" Then Exit Sub 'Pressed cancel
Set oFSO = CreateObject("Scripting.FileSystemObject")
arrText = Split(oFSO.OpenTextFile(strFilePath).ReadAll, vbCrLf)
For Each varText In arrText
strLine = Application.Trim(varText)
If IsNumeric(Left(strLine, 1)) Then strResult = strResult & vbCrLf & Trim(Right(Replace(strLine, " ", String(255, " ")), 255))
Next varText
If Len(strResult) > 0 Then
strResult = Mid(strResult, Len(vbCrLf) + 1)
'At this point strResult contains the data you want
'I have used a MsgBox to verify
'However, you could output the data in any way you see fit
MsgBox strResult
'To put the result in an array, simply re-purpose arrText:
arrText = Split(strResult, vbCrLf)
MsgBox Join(arrText, vbCrLf) 'Again, I used a MsgBox to test its output
Else
MsgBox "No data found"
End If
End Sub