+ Reply to Thread
Results 1 to 10 of 10

Using VBA to copy and name a worksheet

  1. #1
    Registered User
    Join Date
    10-27-2008
    Location
    Southampton, UK
    Posts
    12

    Using VBA to copy and name a worksheet

    I'm trying to use VBA to automatically copy a worksheet (within the same workbook) and then give that worksheet a name dictated by the contents of a cell. I have pulled together some code by searching forums etc but it just ain't working...

    Code is:

    Please Login or Register  to view this content.
    The line that appears to be causing problems is highlighted in red...

    Thanks in advance to anyone who can solve this!
    Last edited by foxtrotdelta; 10-30-2008 at 12:19 PM. Reason: Correcting some of the code which I realised I'd got wrong.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    Perhaps like this?
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-27-2008
    Location
    Southampton, UK
    Posts
    12
    Hmmm - well, it seems to like it more, but it's now giving me the following error:

    Run time error '1004':

    Select method of Range class failed

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Try changing
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Edit: Never mind that either ... :embarassed:

    Third time's the charm?
    Please Login or Register  to view this content.
    Assuming this works, there's a bug in Excel that will cause it to fail after copying some number of sheets -- 10, 20, ...

    If that's a problem, there are other options.
    Last edited by shg; 10-30-2008 at 01:17 PM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    10-27-2008
    Location
    Southampton, UK
    Posts
    12
    And now I have a new error!

    Please Login or Register  to view this content.
    ... is causing the problems:

    Run-time error '1004':
    Application-defined or object-defined error
    However, once the debugger has run and been closed, it seems to (almost) work with the worksheet. I say 'almost' because it makes one too many copies of the worksheet '1', with the final one getting called '1 (2)'...

    Any more ideas??

    Thank you!!

  6. #6
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Exclamation

    foxtrotdelta,

    This error occurs when your code is in a module and the variables used are not defined in that same module.

    This code does nothing if it is defined in Worksheet Code:
    Please Login or Register  to view this content.
    , but it does when defined in a Module

    Questions: Where is your code located
    Are you variables Local or Public

    Try to find a solution in this direction.

    Hope it helps.
    Last edited by rwgrietveld; 10-31-2008 at 05:40 AM.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  7. #7
    Registered User
    Join Date
    10-27-2008
    Location
    Southampton, UK
    Posts
    12
    Hi rwgrietveld,

    Thanks for the advice, but, to be honest, I have no idea what any of what you said means! Part of the problem is that I have had no VB training or anything and am trying to piece something together...

    Any chance you can explain/pose your questions in layman's terms...?

    Thanks.

    fd.

  8. #8
    Registered User
    Join Date
    10-27-2008
    Location
    Southampton, UK
    Posts
    12
    Right - I've looked a bit more at the code, trying to figure what the jiggery is going on and I reckon that this line is the culprit:

    Please Login or Register  to view this content.
    Not because it's a bad bit of code, but because the section of the range from U3 downwards contains a formula - =if(A3="", "", U2+1) - so the code keeps looking down the cells because there is a formula there, even if the formula prints "" into the cell.

    If you see what I mean.

    So, is there a way to get the .End(xldown) section of the code only look for the cells where my excel formula has left some actual text, rather than any cell where there's a formula??

    Thanks again... fd.

  9. #9
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    re: Using VBA to copy and name a worksheet

    Now is the time to attach (upload) your workbook and we'll try and fix it for you.

  10. #10
    Registered User
    Join Date
    10-27-2008
    Location
    Southampton, UK
    Posts
    12
    Thanks to everyone for their help on this but I have now found a different way around the problem I had. Or rather a colleague found a different way around it.

    I appreciate the time and effort though, thanks again...

    fd.

+ 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