+ Reply to Thread
Results 1 to 9 of 9

Importing data through Visual Basic

  1. #1
    Registered User
    Join Date
    01-26-2008
    Posts
    9

    Importing data through Visual Basic

    I have a project in which I need to create a VBA user form which will import data and put it in a new sheet so that it can be manipulated and I have no idea how to do this. The data file must be user selectable from their hard drive so the way I was considering was launching the data import wizard from within the user form, but I have no idea how to do this or if this would be the easiest/neatest solution.

    Any help would be appreciated

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello B_Taylor,

    Welcome to the Forum!

    The following line of code invokes the Import Text File Wizard. This has to be called from a VBA macro.
    Please Login or Register  to view this content.
    This first brings up the Open Text File dialog. The user can then select the folder and file from on their machine. It then launches the Import Wizard.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    01-26-2008
    Posts
    9
    Thanks a lot, but the code gives an "Invalid Outside Procedure" error. Any idea what I'm doing wrong?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello B_Taylor,

    The code needs to be placed in Sub procedure and placed in a Standard VBA module before it be run manually or by code.
    Please Login or Register  to view this content.
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    01-25-2008
    Location
    The Netherlands
    MS-Off Ver
    2002, 2003, 2007
    Posts
    94
    Hi there,

    you need to put the code inside a VBA procedure (macro) to make it work
    e.g.

    Please Login or Register  to view this content.
    you can then run the macro through Tools -> Macro -> Macros
    Or you can create a button in the sheet on which you can click to run the macro.

    More important however is what type of data you want to import.
    The above macro assumes you import .txt files, but is that what you are looking for?

    J.
    Last edited by Leith Ross; 01-26-2008 at 08:11 PM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello jevni1974,

    Please follow the rules about wrapping your code when you post. I did it for you this time. If you forget next time I will lock the post.

    Rules

    Sincerely, Leith Ross

  7. #7
    Registered User
    Join Date
    01-26-2008
    Posts
    9
    Leith Ross:
    Ah, of course, I forgot to put it in a sub. Feel pretty stupid now Thanks a lot!


    Quote Originally Posted by jevni1974
    The above macro assumes you import .txt files, but is that what you are looking for?
    Preferably a .xls file, but I can just export its data to a .txt file if I need to.

  8. #8
    Registered User
    Join Date
    01-25-2008
    Location
    The Netherlands
    MS-Off Ver
    2002, 2003, 2007
    Posts
    94
    hi B_taylor,

    maybe the link below can help you getting some inspiration on how to import data from other excel sheets:

    http://www.ozgrid.com/forum/showthread.php?t=46717

    Hope it helps
    Last edited by jevni1974; 01-26-2008 at 09:34 PM.
    J.

  9. #9
    Registered User
    Join Date
    01-26-2008
    Posts
    9
    Hiya guys,

    The solution you posted is working brilliantly, I just want to know if there is a way to force which cell it goes in rather than have the user select it, since I want it to go into a hidden sheet called "Trend_Calculation" and make it automatically go into cell A1.

    Any idea how I can make it do this?

+ 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