user1204868
user1204868

Reputation: 606

form to open a text file for view in excel

I'm required to create a user form like the one in the picture

enter image description here

I need to open a file with an extension of .txt from excel using VBA and also the button provided.

After selecting the file, when i press start it will open the file.

I was given some codes done by previously but was not working.

will appreciate if someone can help me with this. Thanks!

Public Sub CommandButton1_Click()
 Unload Me 'Cancellation command
End Sub


Private Sub CommandButton2_Click()
'start button
    Application.ScreenUpdating = False

    Sheets("Summary").Select

    Call Transposer("Summary Transpose")

    Sheets("Failing Patterns").Select

    Call Transposer("Failing Patterns Transpose")

    Me.Status = "Status: Finished"

    Me.Error = ""

    'Make sure the screen updates before the end


    Application.ScreenUpdating = True


End Sub


Public Sub Label1_Click()

End Sub

Private Sub testFinder_Click()

    Me.testDirectory.Value = Application.GetOpenFilename

End Sub

Public Sub UserForm_Click()

End Sub

Upvotes: 1

Views: 3546

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149287

To Select the Text File Put this code in the testFinder_Click()

Private Sub testFinder_Click()
    Dim fileToOpen

    fileToOpen = Application _
    .GetOpenFilename("Text Files (*.txt), *.txt")

    If fileToOpen = False Then Exit Sub

    testDirectory.Value = fileToOpen
End Sub

To open the text file you can use this code.

Private Sub CommandButton2_Click()
    '
    '~~> Rest of Code
    '
    Workbooks.OpenText Filename:=testDirectory.Value, Origin:=437, _
    StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
    , Space:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
    '
    '~~> Rest of Code
    '
End Sub

Upvotes: 1

Related Questions