【发布时间】:2019-12-04 13:08:01
【问题描述】:
我有一个名为inFile 的txt 文件。使用 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
【问题讨论】: