1

This is my first time coding anything in VBA - I am trying to write a short macro which reads a file and separates bits and pieces into either column west or column east.

All of it could be wrong or it could just be something simple, but at the moment it won't even recognize my do loop. Any help is much appreciated.

    Private Sub seperateTextFile()
    Dim file As String
    Dim text As String
    Dim textLine As String

    Dim west As Boolean
    west = True

    file = ".alltxt"

    Open file For Input As #1

    Do Until EOF(1)
        Line Input #1, textLine
        text = textLine
        If InStr(text, "HMW") <> 0 Then
            west = True
        If InStr(text, "other") <> 0 Then
            west = False
        If west = True Then
            Sheets("Sheet2").Range("West").End(xlUp) = text
        If west = False Then
            Sheets("Sheet2").Range("East").End(xlUp) = text
    Loop

    Close #1

    End Sub
1
  • 1
    Unless If {test} Then {Action} is all on one line it requires an End If Commented Jun 25, 2016 at 22:38

1 Answer 1

1

I think you are missing your elesif where you have ifs. And you need an End if

Dim file As String
Dim text As String
Dim textLine As String

Dim west As Boolean
west = True

file = "c:\temp\a.alltxt"

Open file For Input As #1

Do Until EOF(1)
    Line Input #1, textLine
    text = textLine
    If InStr(text, "HMW") <> 0 Then
        west = True
    ElseIf InStr(text, "other") <> 0 Then
        west = False
    ElseIf west = True Then
        'Sheets("Sheet2").Range("West").End(xlUp) = text
    ElseIf west = False Then
        'Sheets("Sheet2").Range("East").End(xlUp) = text
    End If
Loop

Close #1

And your file = ".alltxt" for me needed to have an actual path and name such as file = "c:\temp\a.alltxt"

Sign up to request clarification or add additional context in comments.

1 Comment

They are not exclusive so I needed to move them all onto individual lines as the above comment found. The file path I had removed on purpose but thank you for pointing this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.