您需要采取的步骤:
- 打开文件
- 读一行
- 确认该行是 360 个字符
- 将行的块分配给不同的变量
- 用变量做事
- 读取另一行并重复直到 EOF
1 和 2:
您的工作簿需要对 Microsoft Scripting Runtime 的引用才能让您访问 FileSystemObject。我会让你研究一下。
创建一个 FileSystemObject 并使用它来创建一个带有文件路径的 TextStream。
currentLine = textStream.ReadLine()
Do Until textStream.EOF
If Len(currentLine) = 360 Then
firstChunk = Mid$(currentLine, 25, 10)
secondChunk = Mid$(currentLine, 36, 11)
thirdChunk = Mid$(currentLine, 48, 30)
fourthChunk = Mid$(currentLine, 78, 30)
' Do stuff with chunks
End If
currentLine = textStream.ReadLine()
Loop
在适当的时候,您可能会喜欢并有一个数组,其中包含配对项目,详细说明一个块的起点以及它有多少个字符,例如:
Dim arrChunkPoints As Variant
Dim arrChunks As Variant
arrChunkPoints = Array(25,10, _
36,11, _
48,30, _
78,30)
ReDim arrChunks(UBound(arrChunkPoints)\2) ' Integer returned
这将允许您跳过 arrChunkPoints 中的项目,并使用 Mid$() 用 currentLine 的一部分填充 arrChunk 的每个元素,但填充来自 arrChunkPoints 的值。但这可能是另一天。