+ Reply to Thread
Results 1 to 8 of 8

Open a Text File that is named in another cell, copy and paste all in another tab

  1. #1
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Open a Text File that is named in another cell, copy and paste all in another tab

    I need some VBA code that will open a text file in a know location (path) and find that file based on a variable number found in the wordbook Tab1. Once the text file is open I want the contents copied and then pasted into A1 of another tab.

    The attached samples will help you understand what I am doing. The file named: CopyPasteTextFileData.xlsx is where the VBA code will reside. I want the VBA code to use the number found in the specific cell B4 on Tab1 of the workbook. Use that number in cell B4 to open the second attached file: Output"number".txt , where the "number" is the value in Cell B4. Once the text file is found, open it, select the entire contents of the file, copy it and paste it starting at cell A1 of Tab2 of the Workbook. If the code cannot find the exact text file it just returns and error message "Cannot find Output"number".txt.

    The text file is in this path: C:\Users\Bob\Documents\TextFiles

    The sample text file is Output123456.txt where the "123456" is the variable found in cell B4 of the Tab1 tab.

    Thanks in advance!!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    Try this

    Please Login or Register  to view this content.
    Kind regards
    Leo

  3. #3
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    Quote Originally Posted by LeoTaxi View Post
    Try this

    Please Login or Register  to view this content.


    Kind regards
    Leo
    Thanks Leo, I will give it a try.

  4. #4
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    Quote Originally Posted by LeoTaxi View Post
    Try this

    Please Login or Register  to view this content.
    Kind regards
    Leo
    Hi Leo, I am getting a question from excel when the .txt file is closed "There is a large amount of information on the clipboard. Do you want to be able to paste this information into another program later?" I need to answer the question "Yes" so I can paste the data into Tab2. Is there a way of automatically answering the question with "Yes"

  5. #5
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    With this change, and no emty rows or columns in txt file

    Old
    Please Login or Register  to view this content.
    New
    Please Login or Register  to view this content.
    Kind regards
    Leo

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    Here's my effort.

    Please Login or Register  to view this content.
    David
    (*) Reputation points appreciated.

  7. #7
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    Quote Originally Posted by LeoTaxi View Post
    With this change, and no emty rows or columns in txt file

    Old
    Please Login or Register  to view this content.
    New
    Please Login or Register  to view this content.
    Kind regards
    Leo
    Hi Leo, I got it working. What I did to get rid of the "Large Clip Board" message is since the data is limited I just select 100 lines of the data and the message goes away. Thanks again for your help.

  8. #8
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Open a Text File that is named in another cell, copy and paste all in another tab

    Is good as long if there are now more rows then 100
    if CurentRegion dont work cause of emty rows or columns you can count the rows

    Please Login or Register  to view this content.
    this way you can replace the 100 by lastrow

    kind regards
    Leo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Open a new file when clicking a cell, and copy text
    By mariur89 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-17-2015, 05:58 PM
  2. Replies: 8
    Last Post: 11-25-2015, 08:14 PM
  3. VBA Automation -Open a Word File, Copy Text, Paste into a Cell
    By AGALLEGOS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2014, 02:03 AM
  4. [SOLVED] VBA code to open many files, copy text, close file, paste text in second work
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 02-11-2014, 12:59 PM
  5. [SOLVED] Need help! Macro to open sequentially named files, copy and paste data into a master file
    By derrickk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-05-2013, 03:35 PM
  6. Replies: 13
    Last Post: 11-29-2012, 08:25 PM
  7. [SOLVED] open variably named file, copy/paste data into consolidation file, open next file in list
    By sllawrence1968 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2012, 09:49 PM

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