Results 1 to 11 of 11

Thread: Solved: Open Txt File and Copy data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Solved: Open Txt File and Copy data

    Hello,

    I need to open a text file and copy the data from it to a master Workbook.

    Here is the code I managed to put together but I am having "Subscript out of range"

    here is my code.

    [VBA]Sub thetry()
    Dim fileToOpen As Variant

    'THIS WILL OPEN THE TEXT FILE I NEED

    fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    If fileToOpen <> False Then

    Workbooks.OpenText Filename:=fileToOpen, _
    DataType:=xlDelimited, Tab:=True
    End If

    'THIS SHOULD ACTIVATE THE NEW WORKBOOK THATS NAMED AFTER THE FILE THAT WAS OPENED
    Windows(fileToOpen & ".txt").Activate '<============I am getting an error here

    'THIS WILL COPY ALL THE CONTENTS FROM THE SHEET WITH THE SAME NAME AS THE WORKBOOK
    ActiveWorkbook.Sheets(fileToOpen).Cells.Copy

    'THIS WILL CLOSE THE WORKBOOK WITHOUT ANY NOTIFICATION AND THE COPIED DATA WILL STILL BE IN MY
    'CLIPBOARD

    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True

    'this will paste the data

    Workbooks("Master").Activate
    ActiveWorkbook.Sheets("Sheet1").Range("A1").Paste


    End Sub
    [/VBA]

    Thanks for the help
    Last edited by fredlo2010; 05-31-2012 at 09:31 AM.
    Feedback is the best way for me to learn


    Follow the Armies

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •