+ Reply to Thread
Results 1 to 12 of 12

Pasting data to a worksheet by opening multiple closed workbooks...

  1. #1
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Pasting data to a worksheet by opening multiple closed workbooks...

    I've got a macro enabled workbook(test.xlsm) where I have created a button and added the below code :
    Please Login or Register  to view this content.
    It basically copies data from two different closed workbooks (one with multiple sheets) without making any changes in them and pastes it to the macro enable workbook in the first blank row in column A.
    The above code seems to be working fine but as I'm not that well versed with VBA I'm not sure how efficient my code is. The real life database1.xlsx and database2.xlsx files will have a lot of data.
    Can anyone help ?
    Attached Files Attached Files

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    Will it always be only those two database workbooks that you want to open or are there more than 2? Will the source worksheet always be the first sheet in each source workbook?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    Will it always be only those two database workbooks that you want to open
    ==== YES
    Will the source worksheet always be the first sheet in each source workbook
    ==== NOT NECESSARILY (assuming you meant to say the destination workbook i.e. where I'm pasting the data)

    There is only one "destination workbook" but it can have multiple worksheets and the sheet where I want to paste the data is fixed. It may be the second sheet where I paste the data but that will not change in future.

    Hope it is clear.
    Last edited by 0Cool; 02-12-2021 at 11:22 AM.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    You don't need two macros. This one should do what you want when you click the button.
    Please Login or Register  to view this content.
    If you are using any event macros, you can insert the Application.EnableEvents lines of code.
    Last edited by Mumps1; 02-12-2021 at 11:48 AM.

  5. #5
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    If you are using any event macros, you can insert the Application.EnableEvents lines of code.
    What do you mean by that ?

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    In the original code you posted, you had "Application.EnableEvents = True" as part of your code which indicates that you have other events macros in your workbook. If you are not using any other macros, you can ignore this. Have you tried the macro I suggested?

  7. #7
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    Have you tried the macro I suggested?
    YES and it works
    Also, I was wondering how can I efficiently add code to check if the database file is already open or not and if it is open the macro aborts.
    Can you help

  8. #8
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    You can check if a workbook is open IN THE SAME EXCEL INSTANCE by trying to set an object reference for it, e.g., Set wb = Workbooks(some_workbook_name), then checking whether wb is Nothing. Better still to check wb's full pathname.

    More generally, there are various ways to check. See this for some alternatives.

  9. #9
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    I've updated my code to
    Please Login or Register  to view this content.
    Where I've created a function IsWorkBookOpen as below:
    Please Login or Register  to view this content.
    But it is doing nothing, it still runs the full code surprisingly and completes the process without any any error even when database1.xlsx and database2.xlsx are open!
    What am I doing wrong here?

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    Try:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    It is still not showing any messages that the file/files are open and if one of the file is closed it still copies data from it. I want the process to abort if any of the files (i.e.database1 & database2) are open and just show me which of them or both are open.

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Pasting data to a worksheet by opening multiple closed workbooks...

    Try:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 04-28-2019, 10:31 PM
  2. Replies: 2
    Last Post: 09-06-2015, 02:57 AM
  3. Formula for Master worksheet to pull data from multiple closed workbooks...
    By Batman24 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-29-2014, 06:24 AM
  4. [SOLVED] Pasting Values From Multiple Closed Workbooks
    By djfatboyfats in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-05-2013, 06:40 AM
  5. Replies: 1
    Last Post: 07-02-2013, 03:42 AM
  6. Copy values from multiple closed workbooks to a summary worksheet
    By teireii in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-27-2012, 02:58 AM
  7. pasting data to closed workbooks
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2009, 04:53 PM

Tags for this Thread

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