需要从文本文件中提取特定的string到Excel工作表中

我需要从文本文件中find一个键值对,并将其粘贴到Excel列中

键值对在文件结尾处有多次出现

  • 密钥=常数的响应代码
  • 价值=不是每次都在不断变化

例:

文本文件 :

Username : admin Old password : qqqq New password : 1111 Security question : 1 Security answer : Mom Response Code: -500 Operation Completed Response Code: -100 .... Response Code: -202 .... .... 

我的代码:

  Dim myFile As String Dim text As String Dim textline As String Dim x As Integer myFile = "C:\test\test.log" Open myFile For Input As #1 Do Until EOF(1) Line Input #1, textline text = text & textline Loop Close #1 x = InStr(text, "Response code") Range("A1").Value = Mid(text, x + 15, 3) 

:我只得到第一个出现,即响应代码:-500我想循环,将查找所有发生,直到文件结尾,并将该内容粘贴到Excel表格A1列。

你的代码的小MODS:

 Sub dural() Dim myFile As String Dim text As String Dim textline As String Dim i As Long myFile = "C:\TestFolder\test.log" Close #1 Open myFile For Input As #1 i = 1 Do Until EOF(1) Line Input #1, textline If InStr(textline, "Response Code:-") > 0 Then Cells(i, 1).Value = Replace(textline, "Response Code:-", "") i = i + 1 End If Loop Close #1 End Sub 

生产:

在这里输入图像说明