簡體   English   中英

Excel VBA:循環瀏覽文本文件以刪除文件中的字符串

[英]Excel VBA: Loop through text files to remove string within file

我有一個開發的Excel加載項,它可以接收電子數據交換文件,並將這些文件中的特定字段導入Excel。 某些文件包含無法處理的數據類型。 我當前的工作流程是搜索並刪除這些文件。 但是,某些文件包含應處理的數據。 我正在尋找一種解決方案,以遍歷每個文件,搜索特定的數據類型並從文本文件中刪除整個部分。

數據示例:

2FRM世界您好! 5DEL壞數據類型6OTH其他數據2FRM插入我5FOR有效數據類型

在此的示例2FRM將是一節的開始。 我想找到5DEL組,然后刪除其中包含的整個字符串/節。 這意味着從2FRM刪除直到下一個2FRM。

修復后的數據示例:

2FRM插入我5FOR有效數據類型

關於如何實現此目標的任何想法?

我建議使用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

如果您只需要一個Excel公式,則可以使用

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

假定數據“ 2FRM Hello World!5DEL壞數據類型6OTH其他數據2FRM插入我5FOR有效數據類型”位於單元格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

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM