【问题标题】:Excel VBA: Loop through text files to remove string within fileExcel VBA:循环文本文件以删除文件中的字符串
【发布时间】:2017-10-06 20:21:36
【问题描述】:

我开发了一个 Excel 插件,它采用电子数据交换文件并将这些文件中的特定字段导入 Excel。某些文件包含无法处理的数据类型。我目前的工作流程是搜索这些文件并删除它们。但是,某些文件包含应处理的数据。我正在寻找一种解决方案来遍历每个文件、搜索特定数据类型并从文本文件中删除整个部分。

数据示例:

2FRM 你好,世界! 5DEL 错误数据类型 6OTH 其他数据 2FRM 插入我 5FOR 有效数据类型

在此示例中,2FRM 将是一个部分的开头。我想找到 5DEL 组,然后删除它包含的整个字符串/部分。这意味着从 2FRM 删除直到下一个 2FRM。

修复后的数据示例:

2FRM 插入我 5FOR 有效数据类型

关于如何实现这一点的任何想法?

【问题讨论】:

  • 到目前为止,您有什么代码可用于此尝试?看看使用 InstrMid 只是为了初学者。

标签: vba excel


【解决方案1】:

我建议使用InStr 函数。我已经多次使用这种方法并取得了巨大的成功。

Public Function FileImport()

    Dim searchStart As String, searchEnd As String, fullString As String, stringEnd As Long, startLoc As Long, endLoc As Long

    searchStart = "5DEL"
    searchEnd = "2FRM"
    'File open/loop code goes somewhere and fullString is set to the data contained within. I'm manually setting this as an example below:

    fullString = "2FRM Hello World! 5DEL Bad Datatype 6OTH Other Data 2FRM Insert Me 5FOR Valid Datatype"

    stringEnd = Len(fullString)

    If stringEnd > 0 Then 'Skip the section if the string blank.
        Do
            startLoc = InStr(1, fullString, searchStart, vbTextCompare) 'Find the piece of text we are looking for.
            If startLoc > 0 Then 'We have found the starting location of 5DEL in the string.
                endLoc = InStr(startLoc, fullString, searchEnd, vbTextCompare) 'Find end of the bad string, starting from the starting location.
                If endLoc > 0 Then 'A starting and ending location were found.
                    fullString = Left(fullString, startLoc - 1) & Mid(fullString, endLoc, Len(fullString))
                Else 'No ending location was found, thus we only need the left part of the string.
                    fullString = Left(fullString, startLoc - 1)
                    Exit Do
                End If
            Else
                Exit Do 'The bad string was not found, exit.
            End If
            DoEvents 'Just so that the application does not become non-responsive. Can be removed.
        Loop
    End If

    Debug.Print fullString
    'Output is 2FRM Hello World! 2FRM Insert Me 5FOR Valid Datatype

    'File open/loop code ends.

End Function

【讨论】:

    【解决方案2】:

    如果你只需要一个 Excel 公式,你可以使用

    =RIGHT(A1,LEN(A1)-FIND("2FRM",A1,5)+1)
    

    假设数据“2FRM Hello World!5DEL Bad Datatype 6OTH Other Data 2FRM Insert Me 5FOR Valid Datatype”在单元格A1中

    或者在 VBA 中

    Public Function CleanData(sData As String) As String
    Dim sFind As String, iLen As Integer
    
        sFind = "2FRM"
        iLen = Len(sFind)
        If Left(sData, iLen) = sFind Then CleanData = Right(sData, Len(sData) - InStr(iLen + 1, sData, sFind) + 1)
    
    End Function
    

    【讨论】:

      【解决方案3】:

      使用 Instr 查找第一个 5DEL。然后使用 Instr 查找后面的第一个 2FRM,并使用 InStrRev 查找前面的 2FRM。

      f = "2FRM KeepMe1 4DEL KeepMe2 2FRM dontKeepMe3 5DEL dontKeepMe4 2FRM dontKeepMe5 5DEL dontKeepMe6 2FRM KeepMe7 4DEL KeepMe8 "
      a2 = InStr(f, "5DEL")
      Do While a2 <> 0
       a1 = InStrRev(f, "2FRM", a2)
       a3 = InStr(a2, f, "2FRM")
       If a3 = 0 Then a3 = Len(f) + 1
       f = Mid(f, 1, a1 - 1) & Mid(f, a3)
       a2 = InStr(f, "5DEL")
      Loop
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        相关资源
        最近更新 更多