Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

capturing specific text from .txt file and passing into XLS file

Status
Not open for further replies.

misubud

IS-IT--Management
May 29, 2002
25
NZ
Hi, Need help/code to capture the IP address from the second line of a text file (below) and pass to a specific cell, ie B1.

have tried to open as csv but file becomes locked. getting unwanted popups.

Help appreciated
--------------------------------------------------------

Pinging H050FHGZ0091.airnz.co.nz [10.203.15.121] with 32 bytes of data:



Reply from 10.203.15.121: bytes=32 time<10ms TTL=128

Reply from 10.203.15.121: bytes=32 time<10ms TTL=128

Reply from 10.203.15.121: bytes=32 time<10ms TTL=128

Reply from 10.203.15.121: bytes=32 time<10ms TTL=128
-----------------------------------------------------------
 
This assumes that the format of the second line consistantly starts with &quot;Reply from &quot; and the IP address is immediately followed by a semi-colon.
Code:
Option Explicit

Sub ImportIP()
    Dim sFile As String
    Dim sText As String
    Dim sIP As String
    Dim iPos1 As Integer, iPos2 As Integer
    'Read From File
    sFile = &quot;C:\Temp\DataFile.txt&quot;
    Open sFile For Input As #1
        Line Input #1, sText
        Line Input #1, sText
    Close #1
    'Strip IP
    iPos1 = 12  'first char of IP
    iPos2 = InStr(1, sText, &quot;:&quot;)
    sIP = Mid(sText, iPos1, iPos2 - iPos1)
    
    Range(&quot;B1&quot;) = sIP
    
    MsgBox sIP
End Sub
 
lines 1 and 3 of text file are blank,
line 4 starts with &quot;reply&quot;
So, what do i need to tweak to make this work for me?

please also advise the use of the two lines;
Line Input #1, sText
Line Input #1, sText
???
 
Just insert a couple more line inputs. The Line Input reads an entire line from a file. Each time a line is read, it is stored in sText, overwriting the variables contents.
Code:
Open sFile For Input As #1
    Line Input #1, sText
    Line Input #1, sText
    Line Input #1, sText
    Line Input #1, sText
Close #1

or 
    
Dim i As Integer 
Open sFile For Input As #1
    For i = 1 To 4
        Line Input #1, sText
    Next i
Close #1
Hope this helps...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top