+ Reply to Thread
Results 1 to 28 of 28

Macro to Copy Cells From One Workbook into Another as Text

  1. #1
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Question Macro to Copy Cells From One Workbook into Another as Text

    Hello,

    I have 86 separate files that analyze account volume in the 86 different sales territories. Each file has four tabs, one for each product category. I need to compile a master for the entire organization and would like to use a macro to do so. The account information begins at row 11 (column a) on each tab in the individual files. The macro would essentially need to take the cells, starting at row 11 and paste their values into the next available row in the corresponding tab (column a) in the Master file. If possible I'd also like the macro to paste back over the copied rows, in the individual files, as values as well, and then save the file under a different name in the same folder (e.g. C:\X\Y\example.xlsx->C:\X\Y\example1.xlsx where example.xlsx would be the original individual file and example1.xlsx would be the individual file with the values pasted over the formulas).

    Thanks in advance for your help!

  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: Macro to Copy Cells From One Workbook into Another as Text

    'WORKBOOKS W/MULTIPLE SHEETS MERGED INTO SHEETS IN CONSOLIDATION WORKBOOK
    Here's a macro for collecting data from all sheets in all files in a specific folder merging into matching sheets.

    The parts of the code that need to be edited are colored to draw your attention.

    ================
    To copy from row11 and then paste as values, you'd need to change this one line of code:
    Please Login or Register  to view this content.
    to these lines of code, this also changes the original workbook to values only:
    Please Login or Register  to view this content.

    To save the original file with other filenames would be a change here:
    Please Login or Register  to view this content.
    to this, perhaps:
    Please Login or Register  to view this content.
    I think this is a patently bad idea, by they way. You're in the process of cycling through files in a folder and then you start adding files to that folder.... I bet this macro would run out of control.

    I'd recommend you have a folder INSIDE this folder called "Values" and you save these new copies into that folder instead.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 12-27-2019 at 10:14 PM.
    _________________
    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
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Quote Originally Posted by JBeaucaire View Post

    I think this is a patently bad idea, by they way. You're in the process of cycling through files in a folder and then you start adding files to that folder.... I bet this macro would run out of control.

    I'd recommend you have a folder INSIDE this folder called "Values" and you save these new copies into that folder instead.

    [/CODE]
    If I'm reading your post correctly, the macro would actually open all of the individual files in the folder and paste their contents as values into the corresponding tab in the master?? If so I could definitely see how saving the new individual (value) files in the folder could be a problem, otherwise, I don't follow how it would.

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    No, the macro is grabbing files "one at a time" from the folder you designate. If the number of files in that SAME folder were to get bigger and bigger as the macro proceeds, it would never end. It would eventually start finding the files you had just added and open them and do the same thing... kind of crazy.

    I suppose you could test each filename before you open it to make sure it doesn't have "1.xlsx" in the filename....

    But I'd still save new files to a different folder to not mix them.

  5. #5
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Hey Jerry,

    I followed the directions for #4 in the linked site and made the edits recommended above. In the process I went with your suggestion and created a "values" folder so the macro would not run endlessly. I then created a master file in the same folder as the data files, opened that one, created 4 tabs, named them as they're named in the individual files, and ran the macro (which is saved in a different file that contains all of my macros) and it returned an error that reads "Run-time error '9': Subscript out of range."

    Here is the code, with the problem (as identified by the debugger) in yellow font:

    Please Login or Register  to view this content.

    Any ideas??


    Thanks again for your help.
    Last edited by JBeaucaire; 03-08-2013 at 01:43 PM. Reason: Added code tags, as per forum rules. Don't forget!

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    "Run-time error '9': Subscript out of range."

    This means an named object cannot be found by the exact name specified. This is often due to simple misspellings, but other times to hidden spaces and such that make tab names LOOK the same but are not when examined very closely. You will need to identify the exact error in sheet tab names, because this is exactly what that error means.

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    My bad, this will probably need to be fixed, too:
    Please Login or Register  to view this content.
    [/CODE]

  8. #8
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Jerry,

    I opened one of the individual sheets, copied the tab names and pasted them as the tab names in the master and I'm still getting the same error....any ideas??

  9. #9
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    I also tried selecting the tabs in the individual files, copying the tabs to the master, and clearing off all the data, so, in theory, the names would be identical and got the same error

  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: Macro to Copy Cells From One Workbook into Another as Text

    send me your master file and a source file giving the error, I'll test it out.

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbooks.

  11. #11
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Is there a way I can send them to you privately?? I'd rather not post the files to a public forum....

  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: Macro to Copy Cells From One Workbook into Another as Text

    One would expect you replace sensitive data with junk data, whether sent privately or posted to the forum. To test this particular problem you could actually replace the data with absolute garbage since it's irrelevant. We're just trying to examine your sheet names and how the macro is coercing them.

  13. #13
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    I gotcha.

    I've attached an individual and master with dummy data in them.

    Thanks again for the help!
    Attached Files Attached Files

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Um, wouldn't your edited macro be in the Master? I don't see it installed there.

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Anyway, I took the two files above and the macro from further up and it worked bingo bango. Need to update this, though, never expect people to skip the top rows/columns, but some peeps, do...
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    No, I put the macro in another file where I store all the macros I use regularly. Do you think this could be the issue??

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Maybe. THe original macro uses a THISWORKBOOK reference, I expected the macro to be installed in the Master. That's why the sheet names aren't matching up, it's looking at your other workbook.

    Be sure to make the edit I noted above.

  18. #18
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Jerry,

    I made the change and dropped that macro into the master file. Worked like a charm!....but it brought up another issue I didn't think of. Is there a way we can paste the value for "Our Avg Share in Top 80%" as a value in the next empty column to the right of opportunity? This number, the cell it's in, and the column it would be pasted to differ across the different sheets, however they are uniform from file to file, meaning in sheet "Category A" (across all individual files) it's in cell M8 and would need to be pasted to column Q, but in sheet "Category C" the value is in cell G8 and would need to be pasted into column K.

    Also, I'm only interested in stores that are in the top 80%. that being said is there any way to edit the macro so that it only copies rows in which the value in the column marked "Cumulative % of Business" is less than .8?? If not no biggie, the issue above is much more important.

    Any thoughts??

    Thanks again.

  19. #19
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Jerry,

    I was able to get the first part done myself, any insight on that second question?? The top 80%?

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Have you tried using the autofilter? You can filter a column of data using the autofilter to only shows rows with a value of .8 or less (is less right?) before you copy and paste.

  21. #21
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    I have not, reason being, I'd have to open all 86 individual files, apply the filter, and then run this macro. If that's the case it's probably just as easy to sort the master by that column and delete out all the accounts that are above .8.

    Thanks again.

  22. #22
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    And yes, "less" is correct. In the individual files the accounts are sorted by volume in descending order, so once that "Cumulative % of Business" column reaches .8, we've accounted for the top 80%.

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Heavens, no, you'd have the macro autofilter for you before the copy command.

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Assuming the column to filter is column 13 (column M) the first row of data is row11 (apply filter to row 10), something like this:

    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Hey Jerry,

    The code you posted for this last part is fine, the problem is the column and row vary from sheet to sheet, which is completely my fault for not making them uniform. That being said I tried to work around it by writing that code into the part where I copy the "Cumulative Share Avg." value in each sheet (because in that part I go sheet by sheet), but it gave me the "Run-Time Error '91': Object Variable or With Block Variable Not Set" error. I've copied the code below with the portion the debugger highlights in yellow text. I think this is because at this point in the code WSMain has not been defined, I'm just not sure how to fix it.....


    Please Login or Register  to view this content.
    Last edited by balandri; 03-13-2013 at 11:49 AM.

  26. #26
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Ok, I changed the code to the following, and it looks like it worked:


    Please Login or Register  to view this content.
    Do you see any potential issues with it?? Also, it runs fairly slow, is it because I'm making it go sheet by sheet??

    Thanks again for all your help.

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

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Maybe this:
    Please Login or Register  to view this content.

    The goal in final tweaking is to remove all "selecting" and "Activating" and send all your commands to the target sheets/workbooks using your variables.

  28. #28
    Registered User
    Join Date
    08-18-2012
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to Copy Cells From One Workbook into Another as Text

    Quote Originally Posted by balandri View Post
    Jerry,

    I made the change and dropped that macro into the master file. Worked like a charm!....but it brought up another issue I didn't think of. Is there a way we can paste the value for "Our Avg Share in Top 80%" as a value in the next empty column to the right of opportunity? This number, the cell it's in, and the column it would be pasted to differ across the different sheets, however they are uniform from file to file, meaning in sheet "Category A" (across all individual files) it's in cell M8 and would need to be pasted to column Q, but in sheet "Category C" the value is in cell G8 and would need to be pasted into column K.

    Thanks again.
    I don't see the part, in your code, where it's doing this....

+ 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