vba 如何从指定行开始读取文本文件数据

如题所述

1、用循环以行读取,让过你不要的行。
2、文件一次性读入,用split以回车换行为分界装入数组,然后从你要的行开始读你的数据。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-02
Dim fso , sFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8, TristateFalse = 0
Const BeginLine = 5 '指定行,请修改
Set fso = CreateObject("Scripting.FileSystemObject")
Set sFile = fso.OpenTextFile( "1.txt", ForReading,TristateFalse) '打开文本文件,请修改
Do While Not sFile.AtEndOfStream
If sFile.Line >= BeginLine Then '从指定行开始读取文本文件数据
MsgBox sFile.ReadLine
End If
sFile.Skipline
Loop

sFile.Close
Set fso = Nothing
Set sFile = Nothing本回答被提问者采纳
第2个回答  2011-04-01
是TXT文本吧,下面代码可以看看,o(∩_∩)o...哈哈
Private Sub Read()
On Error GoTo ErrorHandler ' 错误跳转
Dim txtLine
Dim FileObj
Dim TextObj
Dim FilePath
Dim m As Integer
Dim i As Integer
Dim SS As String
m = 4 ’设定数值
Range("E25:E200").ClearContents

FilePath = Range("文件路径")
Set FileObj = CreateObject("Scripting.FileSystemObject")
Set TextObj = FileObj.GetFile(FilePath).OpenAsTextStream(1, 0)

For i = 1 To m ’ 略过m行
TextObj.SkipLine

Next

SS = TextObj.readline ’读取第m行的文本
Msgbox SS

TextObj.Close
Set TextObj = Nothing
Set FileObj = Nothing
Exit Sub
ErrorHandler:
If Err.Number = 62 Then MsgBox "AAAA", , "AA"
End Sub
第3个回答  2011-04-01
Private Sub Command2_Click()
Dim str As String
i = 1
n = InputBox("请输入要从第几行开始读?")

Open "d:\123.txt" For Input As #1
Do
Line Input #1, a
i = i + 1
Do While Not EOF(1) And i >= Val(n)
Line Input #1, b
str = str & b & vbCrLf
Loop
Loop While Not EOF(1)
Cls
If Val(n) = 1 Then
Print a & vbCrLf & str
Else
Print str
End If
Close
End Sub

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网