+ Reply to Thread
Results 1 to 8 of 8

Copying worksheets within the same workbook

  1. #1
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    Copying worksheets within the same workbook

    Ok,

    Here is what I am trying to do:

    Open a folder and select all excel files in it (done)
    Copy all worksheets of each file into my main workbook (done)
    Copy all the worksheets of my main workbook (except the one that has the macro button) within the same workbook and clear their contents (done)
    Final step:
    Select each original worksheet from my workbook (that has data) and run some calculations and put the results into its copy. (not done!)

    In effect after I copy the sheets into my workbook my wbook tabs looks like

    Sheet1 Sheet2 Console

    Console is my main sheet where I will have the macro button

    After I copy the worksheets within the same workbook I get something like

    Sheet1 Sheet2 Console Sheet1 (2) Sheet2 (2)

    That is fine. I proceed on clearing all the contents from Sheet1 (2) and Sheet2 (2).

    Next I want to go back to Sheet1, run some calculations that select a specific amount of data (I have the calculation in a macro, that is fine) and put the result in Sheet1 (2). That is where I have the problem. It works for the first worksheet but it will not proceed to the rest. Keep in mind that I may have any number of worksheets, not just two.

    I believe the problem is with the "Destination" worksheet and the way I describe it, I think I should create some kind of variable with it but i'm not
    sure how.


    Here is my code so far. It break on the line in red.

    Please Login or Register  to view this content.
    Any ideas? Also if I could rename each worksheet when it is copied to something more intelligent, i.e instead of Sheet1 to become Sheet1 (2) to have it like Sheet1_data or something like that I would be gratefull.

    K
    Last edited by kostas; 07-10-2008 at 11:52 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi,

    I've not studied the code in detail, but I note you're using an index value 'y' in the instruction

    .....Destination:=Sheets(y)

    You don't appear to have set up a y variable. Shouldn't you be using the loop counter 'x' which is indexing the sheets?

    HTH

  3. #3
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    you are correct about the "y" thing, I forgot it there after I was playing around with some ideas. Will remove it from my post.

    Thank you and please expand on your suggestion if possible.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Quote Originally Posted by kostas
    you are correct about the "y" thing, I forgot it there after I was playing around with some ideas. Will remove it from my post.

    Thank you and please expand on your suggestion if possible.
    Hi,

    I simply meant using the following corrected line in place of the red line

    Please Login or Register  to view this content.
    Rgds

  5. #5
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    Quote Originally Posted by Richard Buttrey
    Hi,

    I simply meant using the following corrected line in place of the red line

    Please Login or Register  to view this content.
    Rgds
    Yes I have tried that but it still will not work ;(

  6. #6
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    I have finally solved it!!!

    I tried different codes and I made it work. I even managed to make it better and more efficient in the process.

    So this is the sequence of things the following macro does:

    1) Opens a "file open" window that allows you to select excel files from any folder

    2) Create a worksheet on your destination workbook that has the same name as the one in the excel file(s) you open adding the suffix "_data"

    3) Filters information from all the opened workbook(s) and dumps the data in your destination worksheet(s)

    Please see further comments at the end of this post

    Please Login or Register  to view this content.
    What I would like from you is the following:

    a) since I tried many different approaches I think some of the code in the macro serves no purpose. Any idea which bits I can delete safely?

    b) When creating the new worksheet in my destination workbook I do not use a very elegant approach. I basically copy the worksheet from the opened excel file to the destination workbook, rename it by adding the suffix I want and then clear its contents. Any better way to do that (if it's too complex then it's fine to stay with what I've got)

    b2) How could I avoid specifying the name of the macro workbook (macro file2.xls) because I would like to rename the file without having to edit the code every time?

    c) Any bits you feel I could improve? Any comments?

    I'm happy to have solved this myself! yey!!!
    Last edited by kostas; 07-11-2008 at 09:52 AM.

  7. #7
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    Fixed issues a & b2. Still looking for a more elegant approach and further comments!

    New code
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    Fixed issue B (renaming the worksheet or chart sheet name on the fly). New code below. Apologies for the new replies but it seems the "EDIT" function is lost after a very short amount of time!

    P.S you might notice that I add the suffix _data, then _chart and then replace both with _chart (so from sheet_data_chart it becomes sheet_chart). This was done because if I had maintained the sheet name sheet_data when copying it (the name) to the chart sheet it caused an error as I had two sheets with the same name. I hope this makes sense!

    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)

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