Help with If Statement

S

Stephen Lynch

Ok, I just need a little advice. I have written code to extract part of a
single file that has three headers and save the parts to three seperate
files but I cannot seem to get the if statements right. My code saves the
three files, so I only need help with the if statements or if there is a
between statement.

What I want to do is say Print everything between @@Header1 and @@Header2 to
File1, Print everything between @@Header2 and @@Header3 to File2, and print
everything after @@Header3 to File3.

My if statements are wrong see below:
While Not EOF(F1)
Line Input #F1, st
If Mid(st, 3) <> "Header1" Then
Print #F2, st
Else
If Mid(st, 3) <> "Header2" Then
Print #F3, st
Else
If Mid(st, 3) <> "Header3" Then
Print #F4, st
Else

Here is what the file looks like, I am trying to separate by the headers:

@@Header1
Bill
Jane
Mary
@@Header2
Jones
Smith
Janus
@@Header3
1
2
3

I want this in File1:

Bill
Jane
Mary

I want this in File2:
Jones
Smith
Janus

I want this in File3:
1
2
3


Here's my code. It saves the files but prints everything:


Public Sub SplitBuildFile()

Dim F1 As Integer
Dim F2 As Integer
Dim fInput As String
Dim fOutput1 As String
Dim fOutput2 As String
Dim fOutput3 As String
Dim st As String

fInput = "C:\test1.txt"
fOutput1 = "C:\position.txt"
fOutput2 = "C:\secdesc.txt"
fOutput3 = "C:\customer.txt"

If Len(Dir(fOutput1)) > 0 Then Kill fOutput1 'make sure it doesn't exist
already

F1 = FreeFile
Open fInput For Input As #F1
F2 = FreeFile
Open fOutput1 For Output As #F2
F3 = FreeFile
Open fOutput2 For Output As #F3
F4 = FreeFile
Open fOutput3 For Output As #F4

While Not EOF(F1)
Line Input #F1, st
If Mid(st, 3) <> "Header1" Then
Print #F2, st
Else
If Mid(st, 3) <> "Header2" Then
Print #F3, st
Else
If Mid(st, 3) <> "Header3" Then
Print #F4, st
Else

Close #F1
Close #F2
Close #F3
Close #F4

End If
End If
End If
Wend
 
G

Graham Mandeno

Hi Stephen

How about having a variable FCurrent which is assigned the value of the file
handle of the current output file, depending on which header was last read?

Dim FCurrent as Integer
....
Do Until EOF(F1)
Line Input #F1, st
Select Case st
Case "@@Header1"
FCurrent = F2

Case "@@Header2"
FCurrent = F3

Case "@@Header3"
FCurrent = F4

Case Else
Print #FCurrent, st
End Select
Loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top