Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-从.txt文件中标识和复制行_Vba_Excel - Fatal编程技术网

VBA-从.txt文件中标识和复制行

VBA-从.txt文件中标识和复制行,vba,excel,Vba,Excel,我使用下面的代码逐行遍历一个txt文件,(1)识别一个文本字符串,(2)导入以下50位数字。我想找到从txt文件导入整行的方法,但不确定代码。另外,是否有方法告诉VBA在txt文件中查找第二个或第三个字符串?目前,下面的代码只是定位第一个事件。谢谢 Sub CopyTXT() myFile = "G:\Filings\Test\Test.txt" Open myFile For Input As #1 Do Until EOF(1) Line Input #1, textline

我使用下面的代码逐行遍历一个txt文件,(1)识别一个文本字符串,(2)导入以下50位数字。我想找到从txt文件导入整行的方法,但不确定代码。另外,是否有方法告诉VBA在txt文件中查找第二个或第三个字符串?目前,下面的代码只是定位第一个事件。谢谢

Sub CopyTXT()

myFile = "G:\Filings\Test\Test.txt"
Open myFile For Input As #1

Do Until EOF(1)
    Line Input #1, textline
    Text = Text & textline
Loop

Close #1

Revenue = InStr(Text, "Operating Revenues")

 'This is where I'm confused - not sure how to copy that row, so I simply pick a large value and use a mid formula - would like to change this.
Range("H1").Value = Mid(Text, Revenue + 7, 50)

End Sub
类似这样的情况(假设您要查找的文本在一行上,而不是跨行拆分)

类似这样的情况(假设您要查找的文本在一行上,而不是跨行拆分)


嘿,蒂姆。谢谢,这正是我要寻找的——为了解决另一个问题——是否有代码让VBA查找第二个或第三个文本字符串?嘿@tim。谢谢,这正是我要寻找的——为了解决另一个问题——是否有代码让VBA查找第二个或第三个文本字符串?
Sub CopyTXT()

    Dim myFile, textline
    Dim i As Long

    i = 0
    myFile = "G:\Filings\Test\Test.txt"

    Open myFile For Input As #1

    Do Until EOF(1)
        Line Input #1, textline

        If InStr(textline, "Operating Revenues") > 0 Then
            i = i + 1

            If i = 2 Then
                Range("H1").Value = textline
            End If

        End If

    Loop

    Close #1

End Sub