Selactively read text file

M

Madiya

Hi,
I am trying to read a text file and accept only selective portion of
the same.
Text file will be like this. 1st line is name, 2nd line is code.
madiya
122
raja
269
tata
222
hag
9897

I would like to get the code of the name I select in my variable.
Say If I assign my variable raja, I want to read the text file and get
269,
Similarly, for tata, I get 222 etc. which means find the name in text
file and return the next line
immediatly below the name.

So fat I have below code which reads entire text file.
Sub nametocodes()
Dim LineofText As String
i = 0
Open "C:\Codelist.txt" For Input As #1
Do While Not EOF(1)
i = i + 1
Line Input #1, LineofText
Debug.Print LineofText
Loop
Close #1
End Sub

Pl help me to get only required data from text file.

Regards,
Madiya
 
J

Joel

Try this

So fat I have below code which reads entire text file.
Sub nametocodes()
Dim LineofText As String
SearchName = "raja"
i = 0
Open "C:\Codelist.txt" For Input As #1
Do While Not EOF(1)
i = i + 1
Line Input #1, LineofText
Debug.Print LineofText
if LineofText = SearchName then
Line Input #1, LineofText
SearchValue = Lineoftext
exit do
end if
Loop
Close #1
End Sub
 
M

Madiya

Try this

So fat I have below code which reads entire text file.
Sub nametocodes()
Dim LineofText As String
SearchName = "raja"
i = 0
Open "C:\Codelist.txt" For Input As #1
  Do While Not EOF(1)
    i = i + 1
    Line Input #1, LineofText
    Debug.Print LineofText
   if LineofText = SearchName then
      Line Input #1, LineofText
      SearchValue = Lineoftext
      exit do
   end if
  Loop
Close #1
End Sub









- Show quoted text -

Joel,
Thanks.
Madiya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top