+ Reply to Thread
Results 1 to 11 of 11

Importing an excel sheet to another excel workbook

  1. #1
    Registered User
    Join Date
    01-02-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    48

    Importing an excel sheet to another excel workbook

    Hi

    I am a beginner with vba and I am stuck in trying to import another excel workbook with its location being flexible as the file-name will change.

    I have a dialog bix opening up to select the file. I am trying to import data from one workbook sheet into my master file sheet. I will manipulate the data once in myself through another macro. But as the workbook i am importing is not consistent naming convention or location but will have the same format data in the sheet I want to be able to select the file but the data format in the workbook i am importing from will not change.

    What i am struggling with is the code to import the data from that file. I have imported text files already and this is working but the code does not work for excel files.

    Workbook1 is the data source file i want to import from and Workbook2 is the master file i will be using.

    Workbook1 sheet name: "REPORT"

    Workbook2 sheet name master file: "Forecast"

    It is just the one sheet in the Workbook1 i want to import from and its the only sheet in the Workbook1 and it is all the data as it is in Workbook1 Sheet not any specific range. I will manipulate once i have it in Workbook2.



    This is what i got so far

    Please Login or Register  to view this content.

    Having difficulty in building the next block of code for the import into my Workbook2.



    I am using excel 2010.

    Any help would be most appreciated.

    Thanks.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing an excel sheet to another excel workbook

    Try this code
    Please Login or Register  to view this content.
    Put this code in a standard module of the destination file which contains the tab called "Forecast".
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Importing an excel sheet to another excel workbook

    Hi,

    I've made some modifications to your code. There was a small mistake in the syntax of GetOpenFilename. The below macro opens the workbook you pick and copies the first worksheet from it to the active workbook. Feel free to play with it.
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-02-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Importing an excel sheet to another excel workbook

    Thanks very much this works perfect. It copy the sheet over as is.

    If i wanted the macro to run in a specific sheet already create called "Forecast" how could i do this?

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing an excel sheet to another excel workbook

    Did you try the code i provided, bkeat?

  6. #6
    Registered User
    Join Date
    01-02-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Importing an excel sheet to another excel workbook

    Yes i have now.

    It opens the file I am trying to access, could this be avoided?

    Also i got this error:

    Run-time error'1004':

    To paste all cells from an Excel worksheet into the current worksheet, you must paste into the first cell(A1 or R1C1).

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing an excel sheet to another excel workbook

    What do you mean by this - It opens the file I am trying to access, could this be avoided?

    To avoid getting the 2nd error - change this line from
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-02-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Importing an excel sheet to another excel workbook

    Yes that works your code is perfect.

    What i meant was when i access the sourced data file and select it to import the data that workbook opens up just wondering could this be avoided as i only want the data from it, its only a minor issue.

  9. #9
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Importing an excel sheet to another excel workbook

    You need to close it afterwards. You can do it by pasting this line at the end of Arlette's code. (But before End Sub of course.)
    Please Login or Register  to view this content.
    Last edited by RHCPgergo; 01-03-2013 at 03:11 PM. Reason: grammar / spelling fail ;<

  10. #10
    Registered User
    Join Date
    01-02-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Importing an excel sheet to another excel workbook

    Thanks you everyone so much for the help especially Arlette, I hope i'll be as good as all of you guys in time.

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing an excel sheet to another excel workbook

    Based on your last post in this thread, its apparent that you are satisfied with the solution(s) you've received and have solved your question, but you haven't marked your thread as "SOLVED". I will do it for you this time.

    In future, to mark your thread as Solved, you can do the following -
    Select Thread Tools-> Mark thread as Solved.

    Incase your issue is not solved, you can undo it as follows -
    Select Thread Tools-> Mark thread as Unsolved.

    Also, since you are relatively new to the forum, i would like to inform you that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post which helped you. This adds to the reputation of the person who has taken the time to help you.

+ 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