find a line and return the 4th line from it

Ragavhere

New Member
Joined
Jan 7, 2011
Messages
4
Hi,

I need to access a text file using excel macro and match the line "Variable name : Fcst_Wk" and return the 4th line below it and assign it to a variable. i.e i need the line "Variable value : Wk2" and assign it to a variable. The content of the file is as below.


Variable name : CurrScenario
Server name : chnveltss04
Application name : Plan_UAT
Database name :
Variable value : LE_Wk1

Variable name : Fcst_Mth
Server name : chnveltss04
Application name : Plan_UAT
Database name :
Variable value : Jul

Variable name : Fcst_Wk
Server name : chnveltss04
Application name : Plan_UAT
Database name :
Variable value : Wk2

Variable name : NextScenario
Server name : chnveltss04
Application name : Plan_UAT
Database name :
Variable value : LE_Wk2

Appreciate your help.

Regards,
Ragav.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi Ragavhere,

Try with:

Code:
[COLOR=Navy]Sub [/COLOR]Search_Line_In_TextFile()
Dim CurrL As String

k = 1
Open "c:\YourPath\inputfile.txt" For Input As #1 [COLOR=Green]'Open in Ram inpufile.txt[/COLOR]

Do While Not EOF(1)[COLOR=Green] 'Loop until end of file.[/COLOR]

Input #1, CurrL [COLOR=Green]'Read each line and store it into CurrL variable[/COLOR]
If InStr(1, CurrL, "Variable name : Fcst_Wk") Then
    
    For i = 1 To 4 'Loop to read 4 lines after line matched
       Input #1, Strg 
    Next
    
[COLOR=Green]'When loop finishes, we'll store the current line in Strg variable[/COLOR]
     Cells(k, 1) = Strg
     k = k + 1
  End If

Loop
Close #1 [COLOR=Green]' Close file[/COLOR]
[COLOR=Navy]End Sub[/COLOR]

It will find all lines containing "
Variable name : Fcst_Wk" and will show in first column of
current worksheet the content that is 4 lines below.

Hope this helps,

Regards
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top