从Textpad中提取特定的单词并在Microsoft Excel中编写

时间:2019-05-21 18:59:50

标签: excel vba

我想从文本键盘中提取一些特定的单词后提取特定的数字,然后将其写入到我的excel列中

例如,文本板文件就是这样

INFO CRITERIA is MATCHED. DISPLAY ID 123456 AND AT T=369   
MAY BE MATCHING OR MAY NOT BE
INFO CRITERIA is MATCHED. DISPLAY ID 12345678 AND AT T=3698  
SEVERAL PACKAGES TO BE FOLLOWED
WAIT UNTIL THE PROCESS FINISHES
INFO CRITERIA is MATCHED. DISPLAY ID 123 AND AT T=32
REGARDING THE TIMINGS..

就这样。我只想提取显示ID和时间(T =),并且必须将显示ID和时间放在excel的不同列中。

由于我是VBA的初学者,请提供帮助

编辑:以下是我尝试的代码。请帮助我如何使用数组和for循环。如何读取剩余的行,如果我们使用中间函数,并且如果“ T =”之后的字符在每一行中都不同,则很难正确地获得数字。你能帮我吗?

Sub Extract()
Dim myFile As String, _
    text As String, _
    textline As String, _
    DISPLAY As Integer, _
    TIME As Integer

    myFile = Application.GetOpenFilename()
    Open myFile For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        text = text & textline
    Loop
    DISPLAY = InStr(text, "DISPLAY ID")
    TIME = InStr(text, "AT T=")
    Range("A1").Value = Mid(text, DISPLAY + 10, 8)
    Range("B1").Value = Mid(text, TIME + 5, 6)
End Sub

1 个答案:

答案 0 :(得分:0)

将您的代码重构为添加所需的循环,并处理许多其他问题。

Sub Extract()
    Dim myFile As Variant ' Handle Cancel
    Dim text As String
    Dim textline As String
    Dim idx As Long     ' separate position from data
    Dim DisplayName As String
    Dim DisplayFound As Boolean 'flag for if rw should be incremented
    Dim TimeName As String
    Dim TimeFound As Boolean
    Dim FNum As Integer ' FreeFile returns an Integer
    Dim rw As Long      'counter for output row
    Dim ws As Worksheet ' best to avoid Active objects

    On Error GoTo EH ' ensure file is closed

    ' speed thing up a bit
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    DisplayName = "DISPLAY ID"
    TimeName = "AT T="

    rw = 1 ' start output at row 1
    myFile = Application.GetOpenFilename()
    If myFile = False Then GoTo CleanUp ' handle cancel
    Set ws = ActiveSheet ' or whatever sheet you need

    FNum = FreeFile
    Open myFile For Input As #FNum
    With ws
        Do Until EOF(FNum)
            Line Input #FNum, textline

            DisplayFound = False
            TimeFound = False
            idx = InStr(textline, DisplayName)
            If idx Then ' DisplayName was found
                text = Trim(Mid$(textline, idx + Len(DisplayName)))
                idx = InStr(text, " ")
                If idx Then  'allow for possibility  value is at end of string
                    text = Trim(Left$(text, idx - 1))
                End If
                .Cells(rw, 1).Value = text
                DisplayFound = True
            End If
            idx = InStr(textline, TimeName)
            If idx Then ' TimeName was found
                text = Trim(Mid$(textline, idx + Len(TimeName)))
                idx = InStr(text, " ")
                If idx Then  'allow for possibility  value is at end of string
                    text = Trim(Left$(text, idx - 1))
                End If
                .Cells(rw, 2).Value = text
                TimeFound = True
            End If
            If DisplayFound Or TimeFound Then
                rw = rw + 1  'increment output row
            End If
        Loop
    End With
CleanUp:
    On Error Resume Next
    Close #FNum
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
Exit Sub
EH:
    'Add Error Handling here

    'Then clean up
    Resume CleanUp
End Sub

请注意,一次像这样一个单元格直接写入工作表可能太慢,尤其是在处理许多文件或文件很大的情况下。如果太慢而不能满足您的需求,请考虑过渡到Variant Array方法(SO的许多示例)