I have a txt
file called inFile
. With VBA, I copy it line by line to a new tx
t file called outFile
.
I have an Excel worksheet. In this sheet, I have a string and a number. The number is the line where the new string needs to go in the txt
called outFile
.
95% of my code works.
My problem: After the code copy the lines from the txt
called inFile
to outFile
, and after I copy the new string from the Excel sheet to the right line.
When I go back to copy the other line from the txt
called inFile
to the txt
called outFile
.
The code copied the line that I just replaced with the new string.
I'm looking for a way to skip one line while copying the line from inFile
to outFile
.
P.S.
cell(f,13)
is where the line number for the new string is placed.
cell(f,17)
is where the new string should be placed.
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
If I understand correctly, you always want to write to file #2; either the line from file #1, or the line from Cells(f, 17) if we are at the target line number.
Why not just this 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
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