在VB中读取文本文件中的特定参数并写入Excel

在VB中读取文本文件中的特定参数并写入Excel,excel,vba,Excel,Vba,我将示例文本作为休耕: 未经测试: Private Sub CommandButton1_Click() Dim myFile As String, text As String, textline As String, _ posLat As Integer, posLong As Integer Dim X As Integer, A As String, B As String myFile = "C:\test\IMEI_Nilanka.txt" Open myFile F

我将示例文本作为休耕:

未经测试:

Private Sub CommandButton1_Click()

Dim myFile As String, text As String, textline As String, _
    posLat As Integer, posLong As Integer
Dim X As Integer, A As String, B As String

myFile = "C:\test\IMEI_Nilanka.txt"

Open myFile For Input As #1
Do Until EOF(1)
    Line Input #1, textline
    X = 1

    text = text & textline
    A = "A" & X
    B = "B" & X    
    X = X + 1
Loop
Close #1

posLat = InStr(text, "Reference No.")
posLong = InStr(text, "IMEI NO")

Range(A).Value = Mid(text, posLat + 30, 7)
Range(B).Value = Mid(text, posLong + 30, 20)

End Sub

嘿,蒂姆,非常感谢你的帮助……它工作得很好……非常感谢
Private Sub CommandButton1_Click()

Dim myFile As String, text As String, textline As String, _
    posLat As Integer, posLong As Integer
Dim X As Integer, A As String, B As String

myFile = "C:\test\IMEI_Nilanka.txt"

Open myFile For Input As #1
Do Until EOF(1)
    Line Input #1, textline
    X = 1

    text = text & textline
    A = "A" & X
    B = "B" & X    
    X = X + 1
Loop
Close #1

posLat = InStr(text, "Reference No.")
posLong = InStr(text, "IMEI NO")

Range(A).Value = Mid(text, posLat + 30, 7)
Range(B).Value = Mid(text, posLong + 30, 20)

End Sub
Private Sub CommandButton1_Click()

Dim myFile As String, text As String, textline As String, _
    posLat As Integer, posLong As Integer
Dim X As Long, rw as Range

myFile = "C:\test\IMEI_Nilanka.txt"
Set rw = ActiveSheet.Rows(1)

Open myFile For Input As #1
Do Until EOF(1)
    Line Input #1, textline

    if textline like "*Reference No.*" Then
        Set rw=rw.offset(1,0)
        rw.cells(1).value=trim(replace(textline,"Reference No.",""))
    end if
    if textline like "*IMEI NO*" then
        rw.cells(2).value=trim(replace(textline,"IMEI NO",""))
    end if

Loop
Close #1


End Sub