+ Reply to Thread
Results 1 to 15 of 15

Macro to copy data from one file to another in the same folder

  1. #1
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Macro to copy data from one file to another in the same folder

    Hi guys/gals,
    The task of maintaining salaried time and bonus sheets fell on my lap and I made a goof in my calculation of comp days and need to revise my workbook to catch the error... Only problem is we are already on pay period 4, and I have a bunch of employees who would have to copy everything over.

    I don't know the best way to transfer all their data over to the new file, and was thinking a macro on the new file that asks the user to pick the old timesheet (probably within the same file folder for simplicity) would be the best way to do it. Can anyone point me in the right direction? I'm open to suggestions if there is a better way other than said macro.

    Anyway, the data range for their entries that needs to be copied is:

    Please Login or Register  to view this content.
    As you can see, if I continue to make errors I will need to continue the Pay Period theme above! I'm not very good at translating what I want to do into macro code that works.

    This forum is awesome btw. I'm an excel novice, and I learn something new on here all the time.

    Attached is the Example sheet that I need to copy the data from and the Corrected Sheet that I need the data pasted into.
    Attached Files Attached Files
    Last edited by chichapher; 02-24-2012 at 07:12 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy data from one file to another in the same folder

    Should the data be copied one below the other or in certain columns? Do you have a sample file that you can upload?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    Sure, I've uploaded a sample file of the old version with the incorrect comp day calculations in it.
    Last edited by chichapher; 02-24-2012 at 07:13 PM.

  4. #4
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    Anyone? Bueller?

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy data from one file to another in the same folder

    What is the format for the new file? What will be the headers? Maybe you can attach a sample output file where the data should be copied.

  6. #6
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to copy data from one file to another in the same folder

    What is the error in the original? It might be easier to update the formulas in the existing workbook. Another suggestion for you: it sounds like there could be quite a few of these workbooks and they all contain VBA code. That means if your code ever fails, you have to correct it in each workbook. If instead you create an add-in (just save a blank workbook as an add-in someplace where all users would have access) then add that add-in as a reference in your VBA project, you can do all your coding in one place...and just one place to do updates if needed later on.
    -Greg If this is helpful, pls click Star icon in lower left corner

  7. #7
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    The error in the original is that payroll made me change the period dates, and I forgot to move the comp day formula's when I realigned the pay period dates. Each of my employees keeps their own version, and now I need to make a revision, but would like to have something that can copy all their data over without having to manually do it.

    I have to protect the sheets and password protect them, otherwise it'll morph and grow into Frankenstein once 60+ people get a hold of it and start changing stuff and emailing modified copies around. I learned that the hard way on previous spreadsheets, but it also prevents them from making simple corrections to goofs I make...

  8. #8
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    Quote Originally Posted by arlu1201 View Post
    What is the format for the new file? What will be the headers? Maybe you can attach a sample output file where the data should be copied.
    The new file is exactly the same format, only a few formula's have been moved to different cells.

  9. #9
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    I updated my original post with both versions of the file. The corrected version highlights where one of my errors was on pay period 5.

  10. #10
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to copy data from one file to another in the same folder

    This is how I would replace the formulas in the bad workbooks.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    Greg,
    I opened the file you attached, followed the choose file dialog box to a previous timesheet and got the "Please contact your administrator" error. These sheets were previously protected using the macro I had. Your code above appears to prompt for password, but it didn't when I executed the code, it just defaulted to fail.

    However, if I navigate to the example sheet I posted, it results in Process Complete (the example I posted was unprotected) and it corrects the formula mistakes.

    The one thing missing, however, is that I would like the "Corrected" worksheet to copy all the user entered information from the previous version. It would therefore accomplish 2 goals: correct my goofs and copy user data over from the previous revision. User data range is defined in my first post.

    I like how your code fixes the formula's in the previous version, but doesn't that defeat the purpose of sending out a new version? With 60 plus users, I could run your code to correct each timesheet I receive and then email it back to the users, but I think a faster implementation would be to send out an updated file that copies data from previous version into it.

  12. #12
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to copy data from one file to another in the same folder

    Sorry...as you posted some VBA code originally I took it for granted that you would know how to ammend my code. You'll need to click ALT +F11 to go into the VB Editor and the navigate to the module titled modCORRECTIONS. There, you'll find the code I posted in #10 post above. If the user's copy of the workbook is protected change the value "YOUR WORKBOOK PASSWORD HERE" to match your workbook password. Then remove the apostrophe on this line
    Please Login or Register  to view this content.
    and this line
    Please Login or Register  to view this content.
    If your individual worksheets are protected change the value "YOUR WORKSHEET PASSWORD HERE" to match your worksheet password. Then remove the apostrophe on these two lines of code
    Please Login or Register  to view this content.
    If you want to have the program save the users fixed workbook, then remove the apostrophe in front of "wbbad.save" as well. If you send this file out to your users they can then run the macro from the button and correct the formulas in their own workbooks. If not all worksheets are protected (ie. there are maybe hidden sheets you didn't protect) this code will still fail, so let me know if that's the case and I'll be happy to help you amend this code. But if the structure of your corrected workbook you posted matches the structure of the user's incorrect copies, this is, in my humble opinion, the better solution.

  13. #13
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to copy data from one file to another in the same folder

    Here's the value update solution as well (just so you can see how it could be done...I still recommend using the CorrectFormulas method I posted previously). This has the same ammendment needs as the prior code
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by gjlindn; 02-25-2012 at 01:56 AM. Reason: corrected range...should include rows 14 - 27

  14. #14
    Registered User
    Join Date
    10-03-2011
    Location
    NM, USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Macro to copy data from one file to another in the same folder

    Thanks, almost got it working.

    is ".Calculation = lCalc" the same as ".Calculation = x1CalculationAutomatic"? Or does it simply refer to the last setting?

    I can't seem to figure out the syntax used to copy the data over...I see the cell range referenced, but I miss the function to copy it (not that it matters, but I'm trying to understand how it works) I'd like to copy B1:B3 from the master tab, but it appears your code references the range on every sheet.

    Can you point me in the direction of a good place to learn Visual Basic for Excel? I never took VB is college, and am kinda regretting it now, but there's got to be some good references available with practical examples.

    Thanks for your time man, really appreciate it!

  15. #15
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to copy data from one file to another in the same folder

    Hi chichapher. lCalc is a variable I use to store the setting of Application.Calculation and then restore the original setting of Application.Calculation back to what is was. Not everyone has autocalc turned on, so it's not a good idea to mess with their settings. Doing it this way allows you to see what they had and then put it back the way it was.

    The code I posted doesn't use a copy/paste method. It sets values of one range = to values of another range. This is the line doing the work
    Please Login or Register  to view this content.
    If you want range B1:B3 copied from the master tab to each of the other tabs alter this section
    Please Login or Register  to view this content.
    I really enjoyed John Walkenbach's Power Programming With VBA (I read the Excel 2003 edition, but you can get 2007 and 2010 as well). Once you start coding, this forum is one of the best resources you'll find.

    PS. I'm a 1989 Hobbs High grad Always happy to help a fellow New Mexican!
    Last edited by gjlindn; 02-28-2012 at 01:18 AM.

+ 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