+ Reply to Thread
Results 1 to 13 of 13

Copy/paste data from one workbook to another but workbook name variable

  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Copy/paste data from one workbook to another but workbook name variable

    I want to create a macro that will copy/paste data from one workbook to another. The problem is that the name of one of the workbooks changes on a daily basis. Is there anyway to write the code so that it will refer to a workbook that has certain words? For instance one something like this

    Please Login or Register  to view this content.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/paste data from one workbook to another but workbook name variable

    The trick is to replace the changing workbook reference with a static workbook reference in a variable that doesn't change. You would set the value of that variable at the moment the macro actually opened the workbook.

    Please Login or Register  to view this content.

    Now, all through the rest of the macro you replace Workbooks(??????.xlsx) with simply MyWB.

    Also, there's almost never a need to "activate" a workbook or worksheet to transfer data into it.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy/paste data from one workbook to another but workbook name variable

    Don't know if this will help or not?

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Copy/paste data from one workbook to another but workbook name variable

    JBeaucaire, how would I program the macro to open the workbook if the name is always different? I usually get emailed the spreadsheet which is only different because of the date.

    John Davis, your solution works for me. The only thing is that I would have to use this macro for each step of copy and pasting. I would rather define a variable like JBeaucaire suggested. I tried programing this macro, but it won't run. It combines your idea with defining a variable instead of selecting the sheet. It errors when I define HAFA_Bank as the workbook.

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy/paste data from one workbook to another but workbook name variable

    Maybe:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy/paste data from one workbook to another but workbook name variable

    I noticed your reply in post #4 that only the date will change then if you use Jerry's method maybe this variation, which open a workbook with todays date (depending on the format used)

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Copy/paste data from one workbook to another but workbook name variable

    I'm going to play with your solution this morning. Problem with the date idea is that it is always referring to the last business day, not the current day. This gets messy when taking into account weekends. I like the solution of Left(wb.name, 17) = "HAFA QA Reporting". I just want to define the workbook as a new variable so that I can switch back and forth throughout the macro easily without a lot of code.

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy/paste data from one workbook to another but workbook name variable

    Post# 5 sets a variable for Sheet1 in Workbook(HAFA QA....). If that Sheet has the same name each day, and you'll be copying/pasting from or too that specific sheet, then you could use that variable (ie. wsHAFA) throughout your code. It will refer back to Workbook(HAFA..).wsHAFA each time.

  9. #9
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Copy/paste data from one workbook to another but workbook name variable

    Looks like I was defining the variable in the wrong order before. I need to put the variable = activesheet instead. I think your solution could work great. I just need time to try it.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/paste data from one workbook to another but workbook name variable

    Once you "set" a worksheet object, you don't need to refer to its parent any longer, the object has been explicitly identified and the parent is now implicit.
    Also, if the name of the unopened workbook has to be "discovered", then we'd use another variable to do that first.


    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Copy/paste data from one workbook to another but workbook name variable

    Wow. I'm really gonna have to play with this. I wish work would slow down so that I could perfect my processes.

    I have had success using your suggestions in some shorter macros, so thanks.

    At the moment I'm trying to use this code to look from the right instead of the left. Any idea why this wouldn't be working for me.

    Please Login or Register  to view this content.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/paste data from one workbook to another but workbook name variable

    Which line of code are you trying to replace with that? Discovering the filename in a folder?

    WB names almost always end .xlsx or .xls or such. It would be unusual of the last 3 letters were "All".


    Perhaps:
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Copy/paste data from one workbook to another but workbook name variable

    Totally forgot about .xlsx! Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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