+ Reply to Thread
Results 1 to 7 of 7

Allow user to open a file and copy the contents?

  1. #1
    Registered User
    Join Date
    08-07-2012
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    3

    Allow user to open a file and copy the contents?

    Hi..I am pretty new to VBA. I am trying to achieve the following but not able to proceed further:

    Environment: Excel 2010
    I have a source file (a.xls)
    Allow the user to open a file (b.xls)
    Copy 3 non-consecutive columns from b.xls to a.xls
    Source columns could include blank cells too

    This is my current code (sourced from internet + by recording a macro):

    Opening a file: (It works !)

    Please Login or Register  to view this content.
    For Copy: (It doesn't work ! As you can see, the file names are hard coded but, in my case, different users will use the macro. So, file names will be different. So, I want to have this flexibility)

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Allow user to open a file and copy the contents?

    Hello there,

    Try replacing your code with the below:
    Note that the first line is outside of the two macros.
    This code is untested so let me know how it works for you.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-07-2012
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Allow user to open a file and copy the contents?

    Hi rvasquez,

    Thanks for the help.

    I am trying to make the code generic i.e. should be able to use with any two workbooks (1st workbook is one with macros and the 2nd workbook is the file opened by the user in 1st macro). Currently, it is tied to qr10021.xlsm and TP_Result Rprt_Tplt.xlsx.

    Can you please help?

  4. #4
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Allow user to open a file and copy the contents?

    The edited code I provided you above should work for that, basically all I did was use variables to reference the workbooks.

    So I set whatever workbook you currently have open to the variable Master WB. Using this code:

    Please Login or Register  to view this content.
    and the workbook that the user selects to open to the variable OpenedWB with this code:

    Please Login or Register  to view this content.
    So after those two lines of code, anytime you see the variable Master WB, it is referencing the first/original workbook you had open. And anytime you see the OpenedWB variable it is referencing the user selected workbook opened.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-07-2012
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Allow user to open a file and copy the contents?

    Many Thanks rvasquez !
    The code provided by you works like a charm !

  6. #6
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Allow user to open a file and copy the contents?

    No problem! Don't forget to mark this thread solved, and maybe give a little star tap if I helped.

    Thanks!

  7. #7
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Allow user to open a file and copy the contents?

    @ man123

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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