+ Reply to Thread
Results 1 to 6 of 6

Copy and rename tabs

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2009
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Smile Copy and rename tabs

    Hi,

    I'm trying to create a macro to insert a new tab and rename it to "Consolidation Assumptions".

    However it keeps failing as the sheet thats being inserted may change its existing name, ie - it may be "sheet 9" then next time i run the macro it may be "sheet 10" depending on what i've done before.

    Does any one know the code i should use?

    Any help appreciated,

    Thanks

  2. #2
    Registered User
    Join Date
    09-18-2009
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Copy nad rename tabs

    This is my code...error occurs at "activesheet.select.name" = line

    Sub Consolidation()
    '
    ' Consolidation Macro
    '

    '
    Sheets("Assumptions").Select
    Sheets.Add
    ActiveSheet.Select
    ActiveSheet.Select.Name = "Consolidation Assumptions"
    Range("A1").Select
    End Sub

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Copy nad rename tabs

    Try

    Sub AddAndNameNewSheet()
        Dim wsNew As Worksheet
        
        Set wsNew = Sheets.Add
        wsNew.Name = "YourSheetName"
        Set wsNew = Nothing
    End Sub

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Copy nad rename tabs

    or perhaps:

    Sheets.Add.Name = "Consolidation Assumptions"
    remember that

    a) you can only have 1 sheet of the same name (case irrelevant) - trying to name a sheet with the same of an existing sheet will throw an error (ie you might want to add a handler)

    b) when a new sheet is added it is immediately the active sheet

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Copy nad rename tabs

    Good point Don,

    I'm going to have to stop rushing, a bit of thought would save me time in the end.

    Cheers

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Copy and rename tabs

    This might be useful if you need to add more than one sheet.

    Option Explicit
    
    Sub AddNewSheets()
        AddNewSheet "Consolidation Assumptions"
        AddNewSheet "abc"
        AddNewSheet "xyz"
        ' etc, etc......
    End Sub
    
    Sub AddNewSheet(strSheetName As String)
        Dim ws As Worksheet
        
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = strSheetName Then
                MsgBox "A Sheet Named" & vbCrLf & vbCrLf _
                        & Chr(34) & strSheetName & Chr(34) & vbCrLf & vbCrLf _
                        & "Already Exists", _
                        vbOKOnly + vbCritical, _
                        "Duplicate Sheet Name Found."
                Set ws = Nothing
                Exit Sub
            End If
        Next
            
        Sheets.Add.Name = strSheetName
        Sheets(strSheetName).Move After:=Sheets(Sheets.Count)
        Set ws = Nothing
    End Sub

    Try it In a new workbook, it will only add the named sheets once.

    Cheers

+ 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