Vba 解析Excel中的数据会导致崩溃
我想知道是否有人知道用Excel VBA解析相当大的数据文件的方法,因为每当我尝试简单的数据解析时,程序就会崩溃。数据的格式是这样的Vba 解析Excel中的数据会导致崩溃,vba,excel,Vba,Excel,我想知道是否有人知道用Excel VBA解析相当大的数据文件的方法,因为每当我尝试简单的数据解析时,程序就会崩溃。数据的格式是这样的 593972,Data,15:59:59.820,9519,9519,Px(25.5),9519,9500,10001,10226,10451,0,0,0,0,0,28.7604,25.4800,25.4841 大约有300万行格式完全相同,如果第一个值(在上面的例子中是593972)是一个特定的数字,我想在这行中提取一些值。我是VBA的新手,因此非常感谢您的帮
593972,Data,15:59:59.820,9519,9519,Px(25.5),9519,9500,10001,10226,10451,0,0,0,0,0,28.7604,25.4800,25.4841
大约有300万行格式完全相同,如果第一个值(在上面的例子中是593972)是一个特定的数字,我想在这行中提取一些值。我是VBA的新手,因此非常感谢您的帮助。非常感谢您抽出时间 尝试使用FSO;修改以满足您的需要
Sub ParseFile()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strLine As String
Dim arrLine() As String
Dim objFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objFile = fso.OpenTextFile("C:\Temp\Text File.txt", ForReading) '<modify path as needed
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.Readline)
If (strLine <> "") Then
arrLine = Split(strLine, ",") 'one dimensional array
'parse the arrLine to test for the data you need
Dim FirstValue as String
FirstValue = arrLine(0)
If FirstValue = "593972" Then
'put the data in Excel if desired/needed
End If
End If
Loop
objFile.Close
Set objFile = Nothing
End Sub
子解析文件()
作为对象的Dim fso
设置fso=CreateObject(“Scripting.FileSystemObject”)
暗斯特林作为弦
Dim arrLine()作为字符串
Dim objFile
读取常数=1,写入常数=2,外观常数=8
设置objFile=fso.OpenTextFile(“C:\Temp\Text File.txt,ForReading)”下面的Sub
打开一个文本流,逐行读取,并验证第一个字段是否每行都有特定值;调整它以满足您的需求:
Public Sub ReadAndValidate( _
ByVal FileName As String, _
ByVal FieldKey As String _
)
' This function doesn't do error handling, assumes that the '
' field separator is "," and that the key field is first. '
' It uses the "Scripting" lib; "Microsoft Scripting Runtime"'
' needs to be referenced by the containing workbook. '
Dim line As String
Dim keylen As Long
Dim fs As Scripting.FileSystemObject
Dim f As Scripting.TextStream
Let FieldKey = FieldKey & "," ' add the separator to the key '
Let keylen = Strings.Len(FieldKey)
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile( _
FileName:=FileName, _
IOMode:=IOMode.ForReading _
)
While Not f.AtEndOfStream
Let line = f.ReadLine()
If Strings.Left$(line, keylen) = FieldKey Then
' replace the statement below with your code '
Debug.Print line
End If
Wend
f.Close
End Sub
您正在分析从写入csv的外部程序生成的内容吗?外部文件是一个.txt文件,是的.txt文件中有大约300万行,但我只想导入前面有特定编号的行。593972
是您想要的前面的编号吗?是的,593972是我想要的前面的编号。谢谢