VBA - 根据前一行

时间:2018-06-08 11:23:35

标签: excel vba excel-vba excel-2016

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

其中一件事是:我标记了 AMT ,在代码创建新行之前, AMT 会被另一个 AMT 值覆盖。避免这种情况的唯一方法是以某种方式在内存中加载2行文本,并根据以前的值进行打印,而不是下一个值。

我可以接受修改 AMT 输入文本文件和输出的代码,然后运行我的代码。

   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

如果需要,我可以放置我的TXT文件的模板,但由于数据的隐私,清理文件需要一些时间。

稍后编辑: 我的TXT例子:

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                                                                        

如果您将代码应用于上部文本,则会产生第5行excel:AMT 354543634 而不是 2845,00 。这种情况正在发生,因为AMT被覆盖并且对我来说是无法实现的,函数 EOF 忽略基于先前字符串的字符串。 行“F **:”总是相同的,所以如果我可以与这一行相关而忽略X行数,它将解决我的情况。

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

非常感谢未来的帮助。

Origin of my code from other source link

0 个答案:

没有答案