+ Reply to Thread
Results 1 to 3 of 3

Importing a text file to an existing sheet, with a dialog to pick the text file

  1. #1
    Registered User
    Join Date
    02-16-2005
    Location
    Rome, Italy
    MS-Off Ver
    2010
    Posts
    40

    Exclamation Importing a text file to an existing sheet, with a dialog to pick the text file

    Hi everyone,

    I have been trying to find information on this but haven't succeeded in all parts. Here is what I want to do in Excel (2003 compatible VBA).

    I have an Excel workbook with 2 worksheets: SEG and IMPORT
    SEG: contains formulas and VLOOKUPs to the Import sheet.
    IMPORT: contains a text file (comma separated), and is not modified in any way from when it is opened.

    I would like a macro to open a dialog window (asking me for the text file), then open the text file and copy it's contents into the IMPORT worksheet of the workbook.

    Some issues:
    • the master Excel Workbook can have different names and be saved in different locations.
    • The text file to be imported can also have different names (hence the dialog)

    Does anybody have an idea how to achieve this or could lend me a hand?
    Thanks in advance, attached a screenshot of the text file opened in Excel (just to give you an idea).

    Best regards,
    Titus
    Attached Images Attached Images

  2. #2
    Administrator JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Importing a text file to an existing sheet, with a dialog to pick the text file

    Since these things are typically affected by some of the choices you make during the manual importation process, turn on the Macro Recorder and let it record you doing the whole process once from beginning to end.

    Post the resulting code here and we can help you edit that to a more dynamic reusable macro. Be sure to put code tags around the posted code:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    13,206

    Re: Importing a text file to an existing sheet, with a dialog to pick the text file

    In '02/'03, there's a command under the Data menu called "import external data." (I haven't yet learned my way around the ribbon in the later versions of Excel, but I'm pretty sure the command is still there somewhere). When you call this command, you get a dialog where you can point it to a text file. then it brings up the text import wizard where you tell it how to import that text file into the spreadsheet. At this point, Excel creates what they call a "query table" or "data range." within the Properties dialog for the created data range are checkboxes to "save query definition" and "ask for file name on refresh." If you make sure both of those options are checked, then you can "refresh" the data (on a schedule or manually according to the other available options) and it will ask for the file whenever it refreshes the data.

+ 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