vba 从文本文件中读取行但跳过前两行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/938796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:48:31  来源:igfitidea点击:

Read lines from a text file but skip the first two lines

excelvbaexcel-vbams-wordtext-files

提问by Kenny Bones

I've got this macro code in Microsoft Office Word 2003 which reads the lines of a text file. The lines each represent a string value that I need to use later in the code.

我在 Microsoft Office Word 2003 中有这个宏代码,它读取文本文件的行。每行代表一个字符串值,我稍后需要在代码中使用它。

However, the first two lines of the text file contains some stuff that I don't need. How can I modify the code so that it skips the two first lines? The "Intellisense" within the VBA editor in Word sucks hard btw..

但是,文本文件的前两行包含一些我不需要的东西。如何修改代码使其跳过前两行?Word 中 VBA 编辑器中的“智能感知”很糟糕,顺便说一句。

Anyway, the code looks something like this

无论如何,代码看起来像这样

Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields as String

sFileName = "c:\fields.ini"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
    MsgBox ("Cannot find fields.ini")
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
    Line Input #iFileNum, Fields

    MsgBox (Fields)

And this code currently gives me all of the lines, and I don't want the first two.

这段代码目前给了我所有的行,我不想要前两行。

回答by Mike Woodhouse

That whole Open <file path> For Input As <some number>thing is so1990s. It's also slow and very error-prone.

这整个Open <file path> For Input As <some number>事情是这样20世纪90年代。它也很慢而且很容易出错。

In your VBA editor, Select References from the Tools menu and look for "Microsoft Scripting Runtime" (scrrun.dll) which should be available on pretty much any XP or Vista machine. It it's there, select it. Now you have access to a (to me at least) rather more robust solution:

在您的 VBA 编辑器中,从“工具”菜单中选择“引用”并查找“Microsoft Scripting Runtime”(scrrun.dll),它几乎可以在任何 XP 或 Vista 计算机上使用。它就在那里,选择它。现在您可以访问(至少对我而言)更强大的解决方案:

With New Scripting.FileSystemObject
    With .OpenTextFile(sFilename, ForReading)

        If Not .AtEndOfStream Then .SkipLine
        If Not .AtEndOfStream Then .SkipLine

        Do Until .AtEndOfStream
            DoSomethingImportantTo .ReadLine
        Loop

    End With
End With

回答by Fionnuala

You can use random access.

您可以使用随机访问。

Open "C:\docs\TESTFILE.txt" For Random As #1 

    Position = 3    ' Define record number.
    Get #1, Position, ARecord    ' Read record.

Close #1

回答by Tomalak

Open sFileName For Input As iFileNum

Dim LineNum As Long
LineNum = 0

Do While Not EOF(iFileNum)
  LineNum = LineNum + 1
  Line Input #iFileNum, Fields
  If LineNum > 2 Then
    DoStuffWith(Fields)
  End If
Loop

回答by user115905

May be I am oversimplifying?

可能是我过于简单化了?

Just add the following code:

只需添加以下代码:

Open sFileName For Input as iFileNum
Line Input #iFileNum, dummy1
Line Input #iFileNum, dummy2
........

Sundar

桑达尔

回答by Sjuul Janssen

Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields as String
Dim TempStr as String

sFileName = "c:\fields.ini"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
    MsgBox ("Cannot find fields.ini")
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum

''//This part skips the first two lines
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr

Do While Not EOF(iFileNum)
    Line Input #iFileNum, Fields

    MsgBox (Fields)
Loop