+ Reply to Thread
Results 1 to 13 of 13

Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

  1. #1
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Hi All,

    Using VBA ... Is it possible when Workbook B is opened to copy a range of data from Workbook A to Workbook B based on two parameters?

    Parameters are:
    Todays Date is greater than 31/03/2018
    Cell B22 = ""

    If both are true open and Unprotect Workbook A and copy Sheet("QTR1") Range("B22:C221 & LastRow") paste to Workbook B Sheet ("QTR2") Range("B22:C221") then close Workbook A

    Any help would be appreciated.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Hi Jalica

    Which code houses the macro...WorkBook B?

    So if in wb B those parameters are met it must open wb A and copy from A to B
    Last edited by sintek; 01-15-2018 at 06:23 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Yes ... the code would be placed in Workbook B to pull data from Workbook A

    Eventually there will be four workbooks (one for each quarter of the year)

    So ...

    Code would be placed in Workbook D to pull data from Workbook C after the 30/09/2018
    Code would be placed in Workbook C to pull data from Workbook B after the 30/06/2018
    Code would be placed in Workbook B to pull data from Workbook A after the 31/03/2018

    I hope I've explained this OK

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    See if this is what you are after...
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Thank you ... I will try this later
    I will obviously need to change the filename to my filename
    Is there anything else that I need to change i.e path details etc

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Path is the path of wb B
    So if wb A is in same path then that remains...
    Change workbook A name to required name.

  7. #7
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Hi Sintek,

    Thanks for your help it has been most useful ... however I am now getting a debug error.

    In ThisWorkbook I have the following code:

    Please Login or Register  to view this content.
    and in Sheet1(QTR2 Audit Tracker) I have the following code:

    Please Login or Register  to view this content.
    I've highlighted the line where the debug error is ...Any Idea how to resolve this?

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    What error appears...And why is this code housed in sheet module....
    what changes must take place before all these macros run...

  9. #9
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    The error I get is Run-time error '9': Subscript out of range

    I've attached the two files so that you can see what they do, if that's ok with you ...

    I am fairly new to VBA coding, my code is probably not the best but it achieves what I want it too.

    Maybe you could suggest an alternative or better way if you have the time to look at it ....


    Many thanks in advance
    Attached Files Attached Files

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    I can only assume it error because the wb that the worksheet_Change event code refers to is not the active workbook...

    Edit...
    Add the red line to your code and see what happens
    Please Login or Register  to view this content.
    Last edited by sintek; 01-15-2018 at 09:29 AM.

  11. #11
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Hi Sintek,

    Tried adding the line of code but it didn't work.
    I think the error is caused because of this:

    When I open wb1 (QTR2) I unprotect it
    Then I open wb2 (QTR1) and unprotect it
    Then I copy from wb2 (QTR1)
    Then I paste to wb1 (QTR2) - This triggers the workbook change event and tries to unprotect wb1 (QTR2) which has already been unprotected

    Is there a way to add something to the worksheet change code below that says if worksheets ("QTR2 Audit Tracker") is protected then unprotect else continue to call subs?

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    09-22-2014
    Location
    Wellingborough, England
    MS-Off Ver
    Microsoft Office 2016 Professional
    Posts
    49

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Hi Sintek,

    Thank you for all your help.
    I think I've now cracked it. I added On Error Resume Next to the Worksheet Change code as below and it now seems to be working as I want it too:

    Please Login or Register  to view this content.

  13. #13
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy & Paste from Workbook A to Workbook B Based on Date and value of cell

    Just remember to add
    Please Login or Register  to view this content.
    after the error checker to disable

    Or make use of code to check sheet protection i.e. Untested
    Please Login or Register  to view this content.
    Last edited by sintek; 01-16-2018 at 01:32 AM.

+ 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. [SOLVED] VBA Macro pull a certain cell in closed workbook and copy/paste into current workbook
    By Hoover5896 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2017, 01:36 AM
  2. Replies: 12
    Last Post: 10-28-2015, 11:48 PM
  3. Copy cell value from 1 workbook to another workbook based on criteria
    By ezonemy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2014, 04:02 AM
  4. Copy range from one workbook, paste to next empty cell in another workbook
    By glock9mm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2013, 12:16 AM
  5. Replies: 6
    Last Post: 01-29-2013, 07:01 AM
  6. Replies: 4
    Last Post: 09-15-2012, 02:18 PM
  7. [SOLVED] copy active sheet, create new workbook, name new workbook based on cell reference
    By jm0392 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-20-2012, 07:12 PM

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