+ Reply to Thread
Results 1 to 29 of 29

Move data & duplicate current sheet

  1. #1
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Move data & duplicate current sheet

    Hello there and thank you very much for any help received in advance. I have only recently started to learn VBA and have been putting it to practice in creating userforms to help people at my work to be able to timetable more quickly.

    I have realised however that in many cases the basic code that I have been using could often be improved. However in the sections below I have not been able to work this out for myself. This needs to be done as after a large number of worksheets exists excel gradually gets slower and slower when running the macros.

    Please could anyone help?

    Macro 1

    Please Login or Register  to view this content.
    Macro 2

    Please Login or Register  to view this content.


    The second of the two macros is the real issue as due to how excel apparently names worksheets the macro will stop working after some time and the only way I have worked out to get it working again is to save and re-open the workbook. It also takes some time to run as I am a complete noob with the dim function.

    Many thanks,

    Tom
    Last edited by TBrooker; 07-14-2009 at 07:16 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    I have found that when the code get a tad slow, Turning off screenupdating speeds it up a lot.

    Try Also removed each Range("D6").Select as it seams to do nothink.
    Please Login or Register  to view this content.
    Ill leave macro 2 for someone else

    D_Rennie

  3. #3
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Cheers for the help I'm not quite sure why I had that select function in there just gotta go through 170 buttons with similar functions to that and change them now .

    Tom

  4. #4
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    Also try
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    re: Move data & duplicate current sheet

    Edit: Ooopps!
    my post was way to slow - I need to type faster ;-)
    Anyway, read on...
    end edit.

    hi Tom,

    There is very little need to use select in optimised code, removing .select makes the code run faster & alllows the code to be shortened as well.

    Macro 1
    Please Login or Register  to view this content.
    I have seen something that explains Excel's approach to creating sheet names & gave a work-around but I can't find it at the moment...
    How many sheets are you copying/inserting?


    To learn more about macros (eg dim & option explicit), have a read of the links in the section "VBA for Beginners" in the below link:
    http://www.excelforum.com/excel-misc...additions.html

    hth
    Rob
    Last edited by broro183; 07-14-2009 at 08:33 AM.
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  6. #6
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Thanks Rob.

    I am creating one sheet at a time, essentially I select the timetable which applies to the student. Select the correct classes using the buttons in the macro and then click "Create" which takes the Name inserted in a NameBox and creates a new sheet with that student's name somewhere on the sheet and with the worksheet's name also being the studen'ts name. There is an intake of between 120-200 students every Monday so that is how many are added to the workbook.

    A new workbook is used each Monday however so it does not become too large.

    Tom
    Last edited by TBrooker; 07-14-2009 at 08:25 AM.

  7. #7
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    I've looked into this problem a bit further but can't find a solution for it. It arises it seems from the fact that although as far as your eyes are concerned the worksheet has the name that you see in the workbook. As far as your VBA code is concerned it has the name displayed in the editor which is similar to as follows Sheet1 (Person's Name), Sheet2 (Person's Name 2) and so on.

    Excel then organises these sheets here as follows:

    Sheet1
    Sheet10
    Sheet100
    Sheet101
    Sheet102
    Sheet103
    Sheet104
    Sheet105
    Sheet106
    Sheet107
    Sheet108
    Sheet109
    Sheet11
    Sheet110

    And so on...

    Therefore when you create the next sheet at one of these weird points where excel has not got the Sheets increasing by one each time the **** hits the fan and a bug appears.

    Any ideas?

    Tom

  8. #8
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    re: Move data & duplicate current sheet

    hi Tom,

    Wow, that sounds like slow going, to create 120-200 timetables!

    I suggest shortening your template sheet name to a single letter eg "z" because there is a limit on the length of sheet names (I think it is 255 characters?) which may cause problems based on the number of sheets being created.

    I'm heading out for a while so hopefully someone else can provide more suggestions based on your feedback...

    Is any more info added to the file after the individual sheets are created or are they just "one offs" eg for printing?
    In other words, is it really neccesary to create a separate sheet for each student?
    Or could you change the layout to have all the students' data on a single "database styled sheet" that is looped through to put the relevant info on one copy of the Template, printed out & then onto the next student (using the same sheet)?
    (ie separate the input section from the sheet creation section)



    hth
    Rob

  9. #9
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    re: Move data & duplicate current sheet

    That shouldn't cause a problem, as you can refer to a sheet by it's name.
    You do have to be careful though, in that if a sheet name contains spaces then you must enclose the name in single quotes when you refer to it.
    Is there any possibility of you uploading a sample workbook, (obviously with fabricated student names)?
    That really is the best way to let people see what the problem might be.
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  10. #10
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    Ive just tested
    Please Login or Register  to view this content.
    I dont realy see a problem with it.
    It creates the new sheet with +1 for the (name) and inserts the page visable Name from namebox.Text. It dosent seam to matter if the sheet (name) are not in order "as if you manually change a sheet (name).

    You can refer to either the sheet (name) or name in VBA so shouldnt affect the coding.

    D_Rennie

  11. #11
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    just noticed you use excel 97 there may be a diffrence as im using 07.
    i dought it though. Maby do what phil_v said and fabricate a workbook.

  12. #12
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Fabricating a workbook now then, will just be a moment.

    The error only starts arising after you have created a fair few worksheets and if you save and reopen the workbook it does not appear for some time again.

    Tom

  13. #13
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Ok have removed all the college details etc from the form and uploaded. You will notice I haven't finished going through and changing the code on each of the Core and Elective buttons yet and am in the process of doing so.

    You'll also notice my severe lack of excel skill very quickly but I only started learning the other day so nevermind.

    Many thanks,

    Tom
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    Ok i just went through and created about 100 new sheet all with unique names and didnt enconuter a problem.

    I got a problem when i tryed creating a sheet using a name that is aready used.
    So i couldnt creat 2 sheet with the name D_Rennie for example. though it is creating the sheet D_Rennie (2)

    is that where you are having issues

  15. #15
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    re: Move data & duplicate current sheet

    So just to clarify;

    1) open the workbook
    2) On the Autofiller select 'Create/Browse Students'
    3) Type a name in the top box, and click create.
    4) Repeat step 3 multiple times and the macro should fail?

  16. #16
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    you can cheek if the sheet exist like this.

    Thats if i got the problem correct?
    Please Login or Register  to view this content.
    Last edited by D_Rennie; 07-14-2009 at 10:58 AM.

  17. #17
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Yes, though you need to return to one of the 4 timetables before hitting create again. Its not just when trying to create a sheet that alreaddy exists though, it is for any name. I will try to get the error to appear again in just a second. Sorry for the space between responses I am running in and out of the office at the moment.

    Edit: Just done as I said above, the exact error I get it as follows:

    Run-time error: '1004':

    Copy method of worksheet class failed


    It always happens after trying to create a worksheet after you have created 11
    Last edited by TBrooker; 07-14-2009 at 11:15 AM.

  18. #18
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    re: Move data & duplicate current sheet

    In that case after creating 12 timesheets still no problem.

    Are you sure you don't have a repeated student name? (ie. you are trying to create a timetable for a student name that is the same as one you already have?

  19. #19
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Yep, do you have an email address so I can email you the original copy. There may be some obscure reason as to why the error doesn't appear in the fabricated version. I am happy to email the spreadsheet to you as the data isn't exactly personal but at the same time I don't want to upload to here as I would prefer the college not be receiving random phonecalls from internet users.

    Tom

  20. #20
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    re: Move data & duplicate current sheet

    Yeah i think i see it Maby with 1997 Usersion Max character length of a worksheet is 11
    Withen 2007 i get the same error is i try to name the worksheet over 32 characters.
    so i went through a have 32 sheets named v right up to vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv addind a v each time and got the error on the 33 v.

  21. #21
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    re: Move data & duplicate current sheet

    Tom, I'd be happy to let you have my email address, but it seems D_Rennie might have it cracked?

  22. #22
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    re: Move data & duplicate current sheet

    Nope afraid not and it is 31 characters in office 97, that is actually to do with the 32-bit coding of the programme. I don't have a problem with naming because each worksheet the maximum number I will reach is around 210 and the names are abbreviated by myself if the student's name is over 31 characters (sometimes they really are that long -.-).

    In other words the numbers of each worksheet is still increasing by one not adding an extra ten, hundred, thousand etc to the sheet number but it is something to do with the fact that in VBA excel organises the sheets in a pattern similar to as if they were.

    So where x = any other numbers 1xxxxx comes before 2x and 3xxxx before 8xx even though 2x is a smaller numbers than 1xxxxx.
    Last edited by TBrooker; 07-14-2009 at 11:53 AM.

  23. #23
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Move data & duplicate current sheet

    I still fail to see it with the sample worksheet.
    Could be somethink on the other side with keeping sheet1, sheet2 so on. And happens once you get a thousands of sheets.
    Email the workbook to [email protected]. Though i think the problem may be over my head.
    Maby get a email off to Phil_V he has a lot more experience than myself

  24. #24
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Move data & duplicate current sheet

    The problem was removed in Excel 2000, I have had even more of a look around.

    The current suggested fix for the problem is to save the workbook and re-open it periodically as I have been doing, but to incorporate this into the macro using a temporary workbook. I am not quite sure how this is all done, I've tried to understand and incorporate what I have found lying around the net in relation to this but to no avail. Also I fear it may add a significant amount of time for the macro to finish running.

    Here is a link to Microsoft's explanation of the problem and their proposed solution.

    Maybe I can use this to convince IT to get around to updating to the newer office version on at least some of the machines.

  25. #25
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Move data & duplicate current sheet

    Looking at that Microsoft page I would be tempted to implement the 'WorkAround' rather than the 'Solution' as I think it would better fit your scenario and not impact your macro running time as much.

  26. #26
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Move data & duplicate current sheet

    Yep, Didnt get the problem withen 2007.

    Though i see why you have it, Tryed the workaround from the Microsoft Website and its a real pain to have to close it and open each time a record is made.
    If i where you ild either upgrade excel, Or Just live with knowing you have to Save,Close,Reopen the worksheet every 7-11 times a sheet is created.

    Not sure if it would work though you could try to handle the error.

    Please Login or Register  to view this content.
    You may even be able to handle the error with the workaround from the microsoft website. "so it only runs on fail not every time"
    Last edited by D_Rennie; 07-15-2009 at 08:14 AM.

  27. #27
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Arrow Re: Move data & duplicate current sheet

    Quote Originally Posted by D_Rennie View Post
    Though i see why you have it, Tryed the workaround from the Microsoft Website and its a real pain to have to close it and open each time a record is made.
    If i where you ild either upgrade excel, Or Just live with knowing you have to Save,Close,Reopen the worksheet every 7-11 times a sheet is created.
    That is why I said it might be better to implement the "WORKAROUND" rather than the "RESOLUTION" proposed on that page, as the "WORKAROUND" avoids the repeated saving, closing and opening of the workbook:

    RESOLUTION
    To resolve this problem, save and close the workbook periodically while the copy...To resolve this problem, save and close the workbook periodically while the copy process is occurring, as in the following sample code:
    Please Login or Register  to view this content.
    Note The number of times you can copy a worksheet before you must save the workbook varies with the size of the worksheet.
    WORKAROUND
    To work around this problem, insert a new worksheet from a template instead of copying an existing worksheet. To do this, follow these steps, as appropriate for the version of Excel that you are running.


    Microsoft Office Excel 2007
    Create a new workbook, and then delete all of the worksheets except for one.
    Format the workbook, and then add any text, data, and charts that you must have in the template by default.
    Click the Microsoft Office Button, and then click Save As.
    In the File name box, type the name that you want for the Excel template.
    In the Save as type list, click Template (*.xltx), and then click Save.
    To insert the template programmatically, use the following code:
    Please Login or Register  to view this content.
    In this code, path\filename is a string that contains the full path and file name for your sheet template.

    Microsoft Office Excel 2003 and earlier versions of Excel
    Create a new workbook, and then delete all of the worksheets except for one.
    Format the workbook, and then add any text, data, and charts that you must have in the template by default.
    Click File, and then click Save As.
    In the File name box, type the name that you want for the Excel template.
    In the Save as type list, click Template (*.xlt), and then click Save.
    To insert the template programmatically, use the following code:
    Please Login or Register  to view this content.
    In this code, path\filename is a string that contains the full path and file name for your sheet template.

  28. #28
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Move data & duplicate current sheet

    I read that though, In this case there are four worksheets that are being used as the sample data.
    And the workaround need the workbook saved as a template with only one sheet.
    Not to say that i cant be done.
    just probley need to valadite the activesheet name as to what template to use.
    i wasnt 100% confident in doing that.

  29. #29
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Move data & duplicate current sheet

    double postdouble post
    Last edited by D_Rennie; 07-15-2009 at 08:44 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