+ Reply to Thread
Results 1 to 10 of 10

Using VBA to copy and name a worksheet

Hybrid View

  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:

    
    Dim WsName As String
        
    Sheets("Pt_Type_Query").Range("U3").Select
    
    Do
    ActiveCell.Offset(l, 0).Select
    
    With Target
        Set WsName = ActiveCell.Value
    End With
        
        Sheets("1").Select
        Sheets("1").Copy After:=Sheets("1")
            
        ActiveSheet.Name = WsName.Value
    
    l = 1
    
    Loop Until ActiveCell.Value = ""
    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?
    Sub x()
    
    Dim WsName As String
        
    Sheets("Pt_Type_Query").Range("U3").Select
    
    Do
        ActiveCell.Offset(1, 0).Select
        WsName = ActiveCell.Value
        Sheets(1).Copy After:=Sheets(1)
        ActiveSheet.Name = WsName
    Loop Until ActiveCell.Value = ""
    
    End Sub

  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
    2010, 2019
    Posts
    40,689
    Try changing
    Sheets("Pt_Type_Query").Range("U3").Select
    to
    Application.GoTo Sheets("Pt_Type_Query").Range("U3")
    Edit: Never mind that either ... :embarassed:

    Third time's the charm?
        Dim cell    As Excel.Range
    
        With Sheets("Pt_Type_Query")
            For Each cell In .Range("U3", .Range("U3").End(xlDown))
                Sheets("1").Copy After:=Sheets("1")
                ActiveSheet.Name = cell.Text
            Next cell
        End With
    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!

    ActiveSheet.Name = cell.Text
    ... 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:
    Sub float()
    Dim MyName As Name
    For Each MyName In Names
      Debug.Print MyName.Name
    Next
    End Sub
    , 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

+ 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