2016-10-15 17 views
0

次のコードを使用して、txtファイルを1行ずつ移動し、(1)テキスト文字列を特定し、(2)次の50桁をインポートします。私はtxtファイルから行全体をインポートする方法を見つけるが、コードがわからない。また、VBAにtxtファイルのSECONDまたはTHIRD文字列を見つけるよう指示する方法はありますか?現在のところ、以下のコードは最初のオカレンスを見つけるだけです。ありがとうございました!!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 

答えて

2

(あなたが探しているテキストが行に分割一行であると仮定していない)、このような何か

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 
+0

@timねえ。ありがとう、これはまさに私が探していたものです。他の質問に対処するために、VBAがテキスト文字列のSECONDまたはTHIRDを探すようにするコードはありますか? – Joe128

+0

上記の私の編集を参照してください –

関連する問題