+ Reply to Thread
Results 1 to 11 of 11

Import data from selected excel file into a specified worksheet

  1. #1
    Registered User
    Join Date
    03-14-2009
    Location
    Victoria
    MS-Off Ver
    Excel 2000
    Posts
    8

    Import data from selected excel file into a specified worksheet

    Hi All,

    I have a workbook which, amongst several worksheets, also includes 12 blank worksheets (one for each month of the year).

    Is it possible to have a macro that will (a) first prompt the user to select an excel file to import (the excel files to import are provided each month and only have one tab); then (b) select which worksheet to import the data (values only) from the selected file into.

    My experience with VB is basic at best so any help is appreciated...
    Attached Files Attached Files
    Last edited by Shogun; 03-24-2009 at 06:48 AM. Reason: Solved

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Import data from selected excel file into a specified worksheet

    Hi

    I'm assuming that the file you want to import has some reference to the Month in question? This is a pre-requisite in order to finding to which sheet the import data should be copied. Otherwise you'll need to ask the user to input the month when they load the file and apply that to a variable.

    First declare a couple of variables to identify your summary workbook and the workbook to be opened.

    e.g.

    Dim wbMyBook as Workbook, wbTemp as Workbook
    Then use
    Please Login or Register  to view this content.
    and later, immediately after the Application.GetOpenFileName

    Please Login or Register  to view this content.
    To prompt the user to open a file just use the

    Please Login or Register  to view this content.
    instruction.

    Now you can use code like

    Please Login or Register  to view this content.
    Finish off with

    Please Login or Register  to view this content.
    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    03-14-2009
    Location
    Victoria
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Import data from selected excel file into a specified worksheet

    Hi,
    Thanks for getting back to me on that - I really appreciated your response.
    You'll have to excuse my lack of knowledge with vb code as I am unable to apply the code you provided.
    If possible, would you be able to supply me with an example spreadsheet/s?
    Also, can you please explain that last bit of code (or actually the second last) that you provided in your response
    Thanks again...

  4. #4
    Registered User
    Join Date
    03-14-2009
    Location
    Victoria
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Import data from selected excel file into a specified worksheet

    Hi,
    Thanks for getting back to me on that - I really appreciated your response.
    You'll have to excuse my lack of knowledge with vb code as I am unable to apply the code you provided.
    If possible, would you be able to supply me with an example spreadsheet/s?
    Also, can you please explain that last bit of code (or actually the second last) that you provided in your response
    Thanks again...

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Import data from selected excel file into a specified worksheet

    Hi,

    I'm attaching an example workbook which prompts you to select a sheet and then asks you to open a file. The macro will copy A1:Z10 from the opened file into the sheet you selected.

    You'll need to adjust the A1:Z10 range as necessary.

    Either click the blue oval on the Jul08 sheet or Ctrl-m for the menu

    HTH
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    03-14-2009
    Location
    Victoria
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Import data from selected excel file into a specified worksheet

    Hi thanks so much for the template, it certainly meets what I require.
    Really appreciate your help...

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Import data from selected excel file into a specified worksheet

    Hi,

    Thanks for the feedback, and glad to be able to help.
    When you have a moment or two would you go back to the original post and mark it SOLVED.

    Kind regards,

  8. #8
    Registered User
    Join Date
    02-14-2012
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Import data from selected excel file into a specified worksheet

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    I'm attaching an example workbook which prompts you to select a sheet and then asks you to open a file. The macro will copy A1:Z10 from the opened file into the sheet you selected.

    You'll need to adjust the A1:Z10 range as necessary.
    Richard,
    I downloaded your sample workbook and I had two questions. What code would allow you change the range from which to copy? e.g. there are ~20 columns but the # of rows of data to import is not constant.

    Also, is there a way to be able to pick which worksheet I'd like to pull the data from? e.g. after opening the file, pick a worksheet to import from.

    Thanks for any assistance you can provide.

    Scott

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Import data from selected excel file into a specified worksheet

    Hello Scott,

    Is the data you want to copy in contiguous rows? i.e. where no row is completely empty? If so you can use the
    .CurrentRegionProperty of the range. So instead of Range("A1:Z10") in the code below, use Range("A1:Z1").CurrentRegion.Copy

    However if the first row of the range to be copied varies the macro will need to be able to determine the first row. Probably with a .Find instruction.

    The file I gave you does record the sheet to which the copy should be made - unfortunately I set the sheet name variable after the other workbook is opened and not before. The code below corrects that.

    Regards


    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-14-2012
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Import data from selected excel file into a specified worksheet

    Richard,
    Yes, it is contiguous rows, so your recommendation worked. I was intrigued by the "PasteSpecial" line in your code. I was wondering if that would allow the formatting (boarders, column width) to be copied as well and how you would use it?

    As for the second part of my previous post, I don't think I was clear enough as to my problem. Is there a way to choose which worksheet to copy from? e.g. I have multiple worksheets in "stFName" it only copies from the last worksheet that is visible when the workbook opens. How would I add the ability to choose which worksheet to copy from? Thanks.

    Scott

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Import data from selected excel file into a specified worksheet

    Quote Originally Posted by navyav8er View Post
    Richard,
    Yes, it is contiguous rows, so your recommendation worked. I was intrigued by the "PasteSpecial" line in your code. I was wondering if that would allow the formatting (boarders, column width) to be copied as well and how you would use it?

    As for the second part of my previous post, I don't think I was clear enough as to my problem. Is there a way to choose which worksheet to copy from? e.g. I have multiple worksheets in "stFName" it only copies from the last worksheet that is visible when the workbook opens. How would I add the ability to choose which worksheet to copy from? Thanks.

    Scott
    Hello Scott,

    Subsequent to a Copy instruction you can choose to PasteSpecial() in several ways
    i.e.
    xlPasteAll
    xlPasteAllExceptBorders
    xlPasteColumnWidths
    xlPasteComments
    xlPasteFormats
    xlPasteFormulas
    xlPasteFormulasAndNumberFormats
    xlPasteValidation
    xlPasteValues
    xlPasteValuesAndNumberFormats


    Re your second Q. Do you know in advance the name of the sheet? If so you could use the
    Sheets("IKnowTheSheetName").Activate
    instruction to select it.

    If you don't know then you'd need some additional VBA code to read all the sheet names and populate a userform listbox with them so that you could then pick one.

    Regards
    i

+ 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