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

时间:2014-05-23 12:19:49

标签: excel vba

我有示例文本作为休闲:

 text....
 text.....
 .....
Reference No:123
IMEI No:785220222
text.....
text.....
 .........
Reference No:456
IMEI No:45666666
text.....
text.....
 .........
我只需要读取给定的参数,例如参考号和参考号。 IMEI否并在两个单独的列中写入excel 我正在使用下面的脚本,但它只提供第一次迭代....请帮助相同。 谢谢你提前..

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

1 个答案:

答案 0 :(得分:0)

未测试:

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