【问题标题】:How can I skip one line in a txt file如何跳过txt文件中的一行
【发布时间】:2019-12-04 13:08:01
【问题描述】:

我有一个名为inFiletxt 文件。使用 VBA,我将其逐行复制到一个名为 outFile 的新 txt 文件中。

我有一个 Excel 工作表。在这张表中,我有一个字符串和一个数字。该数字是新字符串需要进入txt 中的行,称为outFile

我 95% 的代码有效。

我的问题:在将代码从txt 称为inFile 的行复制到outFile 之后,然后将新字符串从Excel 表复制到右侧行。 当我回去将另一行从txt 称为inFile 复制到txt 称为outFile。 代码复制了我刚刚用新字符串替换的行。

我正在寻找一种在将行从inFile 复制到outFile 时跳过一行的方法。

附言 cell(f,13) 是放置新字符串的行号的位置。 cell(f,17) 是应该放置新字符串的位置。

Sub Update_file()

    Dim FolderName As Double
    Dim data1, data2, IfExist, inFile, outFile As String
    Dim f As Integer
    Dim LineNumber As Long

    f = 2
    LineNumber = 0

    Do Until IsEmpty(Cells(f, 2))
        IfExist = "L:\" & Cells(f, 2) & "\COMPANY.bat"
            If Not Dir(IfExist) = "" Then                  
                inFile = "L:\" & Cells(f, 2) & "\COMPANY.bat"
                Open inFile For Input As #1                
                outFile = "L:\" & Cells(f, 2) & "\COMPANY_NEW.bat"
                Open outFile For Output As #2               
                    If Not IsEmpty(Cells(f, 11)) Then                
                        LineNumber = LineNumber + 1
                            If LineNumber = Cells(f, 13) Then
    'aaa:
                                data2 = Cells(f, 17)
                                Print #2, data2
                                LineNumber = LineNumber + 1
                                GoTo bbb
                            Else
                                Do Until EOF(1) Or Cells(f, 13) = LineNumber 
    'bbb:
                                Debug.Print LineNumber
                                Line Input #1, data1
                                data2 = Trim(data1)
                                Print #2, data2
                                LineNumber = LineNumber + 1                            
                                    If LineNumber = Cells(f, 13) Then
                                        GoTo aaa
                                    End If                        
                                Loop                        
                            End If                
                        End If                
                LineNumber = 0
                Close #1
                Close #2                
            Else: MsgBox "äçáøä " & Mid(inFile, 4, 5) & " ìà ÷ééîú"            
            End If    
        f = f + 1    
    Loop    

End Sub

【问题讨论】:

    标签: vba text copy line skip


    【解决方案1】:

    如果我理解正确,您总是想写入文件 #2;来自文件 #1 的行,或者来自 Cells(f, 17) 的行(如果我们位于目标行号)。

    那为什么不只是这个呢?

    inFile = "L:\" & Cells(f, 2) & "\COMPANY.bat"
    Open inFile For Input As #1
    
    outFile = "L:\" & Cells(f, 2) & "\COMPANY_NEW.bat"
    Open outFile For Output As #2
    
    If Not IsEmpty(Cells(f, 11)) Then
        LineNumber = LineNumber + 1
    
        Do Until EOF(1)
            Debug.Print LineNumber
            Line Input #1, data1
            data2 = Trim(data1)
    
            If LineNumber = Cells(f, 13) Then
                data2 = Cells(f, 17)
            End If
    
            Print #2, data2
            LineNumber = LineNumber + 1
        Loop
    End If
    
    LineNumber = 0
    
    Close #1
    Close #2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多