Vba 如何将文本文件中的特定键值提取到excel中
我需要从文本文件中提取字符串到excel工作表中。 我只能获取excel(A1)中第一次出现的字符串ans paste。 现在我需要继续抓取,直到EOF,并将该字符串粘贴到A2、A3、A4中 示例:Vba 如何将文本文件中的特定键值提取到excel中,vba,excel,vbscript,macros,Vba,Excel,Vbscript,Macros,我需要从文本文件中提取字符串到excel工作表中。 我只能获取excel(A1)中第一次出现的字符串ans paste。 现在我需要继续抓取,直到EOF,并将该字符串粘贴到A2、A3、A4中 示例: Private Sub CommandButton1_Click() Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer myFile = "C:\test
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong 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
posLat = InStr(text, "Response Code")
Range("A1").Value = Mid(text, posLat + 15, 3)
End Sub
文本文件多次包含xxx=100键值。
xxx是常数,而值每次都在变化。
所以我需要从文本文件中获取所有xxx值,然后
将其粘贴到每个单独的excel单元格中
我的代码:
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong 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
posLat = InStr(text, "Response Code")
Range("A1").Value = Mid(text, posLat + 15, 3)
End Sub
尝试使用此改进的代码:
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
Dim I as long
myFile = "C:\test\test.log"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
'text = text & textline
text = textLine
posLat = InStr(text, "Response Code")
Range("A1").Offset(I,0).Value = Mid(text, posLat + 15, 3)
I= I+1
Loop
Close #1
End Sub
试试这个:
Option Explicit
Sub main()
Dim myFile As String
Dim valsArray As Variant
Dim text As String, vals As String
Dim iVal As Long
myFile = "C:\test\test.log"
Open myFile For Input As #1
text = Input$(LOF(1), #1) '<--| read all file in a string
Close #1
valsArray = Split(text, "Response Code=") '<--| split text file into bits separated by "Response Code=" string
For iVal = 1 To UBound(valsArray) '<--| loop through generated array skipping its first element
vals = vals & Left(valsArray(iVal), 3) & "," '<--| build values string delimited by a comma
Next iVal
valsArray = Split(Left(vals, Len(vals) - 1), ",") '<--| split values string into an array
Range("A1").Resize(UBound(valsArray) + 1).Value = Application.Transpose(valsArray) '<--| write down the array
End Sub
选项显式
副标题()
将myFile设置为字符串
Dim数组作为变量
变暗文本为字符串,VAL为字符串
长得一样暗
myFile=“C:\test\test.log”
打开myFile作为#1输入
text=Input$(LOF(1),#1)'显示到目前为止的代码。这将很容易提供缺少的指导方针,看到您的code@KazimierzJawor先生,我已经用代码更新了我的帖子。我不知道你的文本文件里有什么,为什么你建议使用text=text&textline
,这可能是多余的。请改用text=textLine
试试。@virajshirsat,你通过了吗?