首页 文章

将特定数据从txt文件导入excel中的特定列

提问于
浏览
-2

我想从.txt文件中导入特定数据,将它们放入Excel工作表中,该工作表不断更新(即)脚本需要将数据放在工作表上的下一个可用行中 .

我的.txt文件的一部分

**Processor Scientific Analysis
    Analysing...
    Aggregated Score : 5.99GFLOPS
    Result ID : Intel(R) Celeron(R) CPU 3965U @ 2.20GHz (2C 2.21GHz, 2x 256kB L2, 2MB L3)
    Speed : 2208MHz
    Capacity : 2Unit(s)
    Finished Successfully : Yes**

在我的Excel表格中,我只需要将总分(5.99)的值放在正确的列上 .

救命?

2 回答

  • 2

    您需要更具体地说明“工作表上的下一个可用行” . 你把它放在第一列吗?

    以下函数将返回字符串 Aggregated Score : 后面的文本

    通过以下内容从子运行:

    gGlopString = getString()

    以下代码通过文件流的每一行中的文本进行检查 . 前19个字符(Len(testStr)的结果与teststr匹配,它将字符串的剩余文本(所有字符在文本行长度的结果右边 - testStr的长度)放入变量中最终由你的功能返回 .

    Public Function getString() As String
            Dim myFSO As New FileSystemObject
            Dim path As String
            Dim fileName As String
            Dim testStr as String
    
            path = "C:\PATH\TO\FILE"
            fileName = "FILENAME.txt"
            testStr = "Aggregated Score : "
            i = 0
            x = 0
    
            Set fso = myFSO.OpenTextFile(path + fileName)
            Do Until fso.AtEndOfStream
                txt = fso.ReadLine
                For x = 1 To Len(txt)
                    If Mid(txt, x, Len(testStr)) = testStr Then
                        resultStr = Right(txt, (Len(txt)-Len(testStr))
                        ' Enter your code to move resultStr into the cell you want
                        Exit For
                    End If
                Next
                i = i + 1
            Loop
    
            getString = resultStr
    
            fso.Close
    
        End Function
    
  • 0

    这将读取指定的文本文件,并将"Aggregated Score"和"GFLOPS"之间的值放入工作表上的 active cell

    Function GetAggregatedScore() As Double
    
        Const txtFileName = "C:\yourPath\yourFileName.txt"
        Dim txt As String, tStart As Long, tStop As Long
    
        Open txtFileName For Input As #1
        txt = Input(LOF(1), #1)
        Close #1
    
        tStart = InStr(1, txt, "Aggregated Score :", vbTextCompare) + Len("Aggregated Score :")
        tStop = InStr(tStart, txt, "GFLOPS", vbTextCompare)
    
        GetAggregatedScore = Val(Mid(txt, tStart, tStop - tStart))
    
    End Function
    

    您可以在工作表中添加带有该代码的按钮,并在需要文件当前版本的编号时单击它(假设文件将始终具有相同的名称,或者根据需要更新名称) .

相关问题