VBA-基于前几行从文本文件中删除行

VBA-基于前几行从文本文件中删除行,vba,excel,excel-2016,Vba,Excel,Excel 2016,我有一个2k多行的大文本文件。目前的代码正在寻找特定的标记,并将其放在一列中。在标记生效日期创建新行 其中一件事是:我有标记AMT,在代码创建新行之前,AMT被另一个AMT值覆盖。避免这种情况的唯一方法是在内存中加载文本中的两行,并基于上一个值打印或不打印下一个值 我可以接受一个代码来输入文本文件并输出AMTfixed,然后运行下面的代码 Sub OpenFilesConvertTexttoCOL() Dim MyFolder As String Dim MyFile As

我有一个2k多行的大文本文件。目前的代码正在寻找特定的标记,并将其放在一列中。在标记生效日期创建新行

其中一件事是:我有标记AMT,在代码创建新行之前,AMT被另一个AMT值覆盖。避免这种情况的唯一方法是在内存中加载文本中的两行,并基于上一个值打印或不打印下一个值

我可以接受一个代码来输入文本文件并输出AMTfixed,然后运行下面的代码

   Sub OpenFilesConvertTexttoCOL()

   Dim MyFolder As String
   Dim MyFile As String
   Dim textline As String

   MyFolder = "C:\Folder\"
   MyFile = Dir(MyFolder & "\*.txt")
   Dim filename As String
   Dim currentrow As Long: currentrow = 2

Do While MyFile <> ""  'This will go through all files in the directory, "Dir() returns an empty string at the end of the list


    filename = MyFolder & "\" & MyFile  'concatinates directory and filename

    Open filename For Input As #1

Do Until EOF(1)  'reads the file Line by line
        Line Input #1, textline
        'text = text & textline
        If (textline = " ") Or _
        (textline Like "*Amount:      Cur:*") Then   'error handler, if line was empty, ignore


        Else
            Dim splitline() As String
            splitline() = Split(textline, ": ", -1, vbTextCompare)
        ' because of how my specific text was formatted, this splits the line into 2 strings.  The Tag is in the first element, the data in the second

            If IsError(splitline(0)) Then
                splitline(0) = ""
            End If

            Select Case Trim(splitline(0)) 'removes whitespace
                Case "Currency"
                    'currentrow = currentrow + 1 'files that didn't have a description row, resulted in empty rows in the spreadsheet.
                   ActiveSheet.Range("A" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(1)), " ")(0) '= splitline(1) ,= Trim(splitline(1)), " ")

                Case "Value Date"
                    ActiveSheet.Range("B" & currentrow).Cells(1, 1).Value = Trim(Right(splitline(1), 42)) 'Trim(Right(splitline(1), 42))
                    currentrow = currentrow + 1 'files that didn't have a description row, resulted in empty rows in the spreadsheet.

                Case "Reffor the A O"
                   ActiveSheet.Range("C" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(1)), " ")(0)

                Case "Amt"
                    ActiveSheet.Range("D" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(1)), " ")(0)


                Case "Ref"
                    ActiveSheet.Range("E" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(1)), " ")(0)

                Case "DCM"
                    ActiveSheet.Range("F" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(2)), " ")(0)


                Case "NO. TR"
                    ActiveSheet.Range("H" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(1)), "VOR")(0)

                Case "Id Code"
                    ActiveSheet.Range("I" & currentrow).Cells(1, 1).Value = Split(Trim(splitline(1)), " ")(0)

                Case "ZGR"
                    ActiveSheet.Range("K" & currentrow).Cells(1, 1).Value = Trim(splitline(1))


                'Case "Supplementary Details"
                    'ActiveSheet.Range("L" & currentrow).Cells(1, 1).Value = splitline(1)

                    '...etc. etc... so on for each "tag"

            End Select

            'different Separators

            splitline() = Split(textline, "XXX-", -1, vbTextCompare)
            Select Case Trim(splitline(0))
            Case "YOUR REF "

                ActiveSheet.Range("G" & currentrow).Cells(1, 1).Value = Trim(splitline(1))
            End Select


           splitline() = Split(textline, "-ZAHLUNG", -1, vbTextCompare)
            Select Case Trim(splitline(0))
            Case "/REMI/AUSLANDS"
                ActiveSheet.Range("J" & currentrow).Cells(1, 1).Value = Trim(splitline(1))
            End Select

        End If
    Loop      
    Close #1
    MyFile = Dir()  'reads filename of next file in directory  
    Loop
   End Sub
如果将应用的代码放在上面的文本上,将生成第5行excel:AMT354543634,而不是2845,00。发生这种情况是因为AMT被覆盖,对我来说,函数EOF不可能忽略基于上一个字符串的字符串。 行“F**:”总是一样的,所以如果我能关联到这行忽略X行数,这将解决我的情况

接受任何其他解决建议,包括提取数据的其他软件

非常感谢今后的帮助


我很想帮你,但是你的问题我一点也不清楚。请参阅此链接创建一个最小、完整、可验证的示例。它将帮助开发人员在将来更好地理解您的问题。@JvdV I更新了一个示例文件,以查看结果和预期结果。如果不清楚,请随时发表评论。我愿意提供信息来帮助自己。我真的不确定你在寻找什么,但是如果你想从上面的文件中获取特定的信息,你应该考虑使用正则表达式,使用<代码>脚本。(您需要在VBA中添加对Microsoft脚本控件的引用,请参阅更多),这是一个可用于测试每一行的示例表达式with@TaylorScott . 我将探讨这种可能性。谢谢你的参考资料:)。
enter code here

     Block 3                                                                        
     Block 4                                                                        
     F20: T R N                                            

     F25: A I - A                                          
     F28C: St Number/Se Number                                         
               Statement Number:          00109                                     
               Sequence Number:          /002                                       
     F60M: O B - D/C - Date - Cur - Amt                    
               DCMark: D/C Mark:          C                                         
               Date:          180606          2000 Apr 01                           
               Currency:          EUR          EURO                                 
               Amt:          21489135,63          #21489135,63#                  
     F61: SSLL                                                            
               Value Date:          180606          2000 Apr 01                     
               DCM: Debit/Credit Mark:          C                       
               Amt:          694,88          #694,88#                            
               Transaction Type:          N                                         
               Id Code:          TRF                                    
               Reffor the A O:          43000180606Z9257           
     Ref:          //034542452450    

               Supplementary Details:                                               


     F86: info22                                              
                /BENM//GT2543534534534   aaa aaaa                   
                /ORDP/aaaa aaa aaaaa 8/18                 
                /REMI/UBERWEISUNG OUR REF: 034564654456                         
               YOUR REF: 43000180606Z9257 S103 FAV Y/ddsdsXXX                    
               ZGR: HHFHD SFOI DNNDS AGT: EUR 694,88                           
               /AT934300030233977005      BIC:VBOEATWWXXX                           
     F61: SSLL                                                            
               Value Date:          180606          2000 Apr 01                     
               DCM: Debit/Credit Mark:          C                       
               Amt:          2000,00          #2000,00#                          
               Transaction Type:          N                                         
               Id Code:          TRF                                    
               Reffor the A O:          3801920873                 
     Ref:          //03GT180606136336    

               Supplementary Details:                                               

                         /TGDFD/EUR2000,00/                                          
     F86: info1                                              
                /BENM//GT39FEMA16126000011201000470400 S-K, HA        
                /ORDP/        
                /REMI/UIS OUR REF: 03GT180606136336                         
               YOUR REF: 3801920873       S103 FAV Y/ddsdsXXX                    
               ZGR: /RFB/WITHDRAWAL       AGT: EUR 2.000,00                         

     F61: SSLL                                                            
               Value Date:          180606          2000 Apr 01                     
               DCM: Debit/Credit Mark:          C                       
               Amt:          2845,00          #2845,00#                          
               Transaction Type:          N                                         
               Id Code:          TRF                                    
               Reffor the A O:          FTT7681198 01              
     Ref:          //014454154564    

               Supplementary Details:                                               

                         /GTF/EUR2845,00/                                          
     F86: info22                                              
                //ewrewtweqterqyfrgdfgaddfag      
                /343253256346456/gfdgadfgasdadf        
                /REMI/UBERWEISUNG OUR REF: 180606192446                         
               YOUR REF: 4354356346                
               ZGR: /fdgafg/3464363434   AGT: EUR 2.845,00                         

     F62M: C B  - D/C - Date - Currency - Amt     
               DCMark: D/C Mark:          C                                         
               Date:          180606          2000 Apr 01                           
               Currency:          EUR          EURO                                 
               Amt:          354543634          #54654656#                  
     Block 5