+ Reply to Thread
Results 1 to 2 of 2

Open Text in Current WorkBook

Hybrid View

  1. #1
    Registered User
    Join Date
    03-02-2008
    Posts
    39

    Open Text in Current WorkBook

    I am trying to open a text file in a current workbook. The code below is opening the text file but it is creating a new workbook. I want to add a new sheet in the current workbook with the contents in the text fle insteasd of creating a new workbook.

    Sub OpenPchFile()
    
    PchFile = Application.GetOpenFilename("Punch Files (*.pch), *.pch")
    If PchFile = False Then
    MsgBox "Stopping because you did not select a file"
    Exit Sub
    End If
    
    Workbooks.OpenText Filename:=PchFile, DataType:=xlFixedWidth, startrow:=7, comma:=False
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Open Text in Current WorkBook

    Try this:
    Sub OpenPchFile()
    
        Dim PchFile As Variant
        Dim PchWorkbook As Workbook
        
        PchFile = Application.GetOpenFilename("Punch Files (*.pch), *.pch")
        
        If PchFile <> False Then
            Workbooks.OpenText Filename:=PchFile, DataType:=xlFixedWidth, startrow:=7, comma:=False
            Set PchWorkbook = ActiveWorkbook
            PchWorkbook.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            PchWorkbook.Close savechanges:=False
        Else
            MsgBox "Stopping because you did not select a file"
        End If
            
    End Sub
    Post responsibly. Search for excelforum.com

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1