7,763
社区成员
发帖
与我相关
我的任务
分享
Dim aryInput() As Byte
Dim strInput As String
Dim strOutput As String
Dim i As Long
Const strFile = "B:\test.txt" ' 文件全路径
Const strKey = "VA=""_NumXPos" '需要查找的特征字符串
ReDim aryInput(FileLen(strFile))
Open strFile For Binary As #1
Get #1, , aryInput
Close #1
strInput = StrConv(aryInput, vbUnicode)
i = InStr(1, strInput, strKey) + Len(strKey)
strOutput = Mid(strInput, i, InStr(i, strInput, """") - i)
MsgBox strOutput
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim strOut As String
For i = 1 To 2
strOut = strOut & i & ":" & getData("B:\test" & Format(i, "000") & ".txt") & vbCrLf
Next
MsgBox strOut
End Sub
Private Function getData(ByVal strFile As String) As String
Dim aryInput() As Byte
Dim strInput As String
Dim i As Long
Const strKey = "VA=""_NumXPos" '需要查找的特征字符串
ReDim aryInput(FileLen(strFile))
Open strFile For Binary As #1
Get #1, , aryInput
Close #1
strInput = StrConv(aryInput, vbUnicode)
i = InStr(1, strInput, strKey) + Len(strKey)
getData = Mid(strInput, i, InStr(i, strInput, """") - i)
End Function
Option Explicit
Private Sub Command1_Click()
Dim objExcelApp As Object
Dim objWorkBook As Object
Dim strTemp As String
Dim i&, k As Long
strTemp = "E:\Temp\Data.txt" ' 此处按你的实际文件路径写
Set objExcelApp = CreateObject("Excel.Application")
Set objWorkBook = objExcelApp.Workbooks.Add()
Open strTemp For Input As #1
i = 1&
Do
If (EOF(1)) Then Exit Do
Line Input #1, strTemp
If ("" < strTemp) Then
If ("VA=""_NumXPos" = Left$(strTemp, 12)) Then
strTemp = Mid$(strTemp, 13)
k = InStr(1&, strTemp, """")
objWorkBook.Sheets(1).Cells(i, 1).Value = Left$(strTemp, k - 1&)
' Exit do ' <--- 如果只有1条数据,就用这句
i = 1& + i
End If
End If
Loop
Close
objExcelApp.Visible = True ' 让Excel窗口显示出来
Set objWorkBook = Nothing
Set objExcelApp = Nothing
End Sub