Consulting

Results 1 to 4 of 4

Thread: Copy Sheet - Change Name with a twist.

  1. #1

    Copy Sheet - Change Name with a twist.

    Dim ws1 As Worksheet
                Set ws1 = ThisWorkbook.Worksheets("Funky")
                ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
    Ok so this does what I want it to do but I can't seem to figure out how to change the name of the copied sheet.

    I can't use: ThisWorkbook.VBProject.VBComponents("Funky (2)".Name = "blah" either. I have no permission to use this and gives an error.

    Is there a way to make exact copy with formatting while being able to change the sheet name?

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    worksheet.Copy is a method that does not return an object so you have to rely on the copied sheet becoming the activesheet, so straight after the copy line have a line to 'capture' the active sheet by assigning an object variable to it with something along the lines of:
    Set NewSht=ActiveSheet
    Thereafter you can do what you will with it, such as:
    NewSht.name = "blah"

    It's very unlikely the user will manage to change the active sheet in the short time between the execution of the Copy and Set lines (in fact I doubt he could if he tried) but check out the vba Help on Application.Interactive and its attendant Remarks sectoin.
    Last edited by p45cal; 06-17-2016 at 03:12 PM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Thank you. Worked like a charm.

    I hope soon I will be more of a contributor than leecher off this site. I need to sit down and learn the "real" way to do this rather than piece things together all the time from searches. This is kind of addicting. Thanks again.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you want user input, get if before the sheet is copied
    Sub test()
    
    
    Dim NewName As String
    Dim ws1 As Worksheet
    
    
    NewName = InputBox("New name")
    Set ws1 = ThisWorkbook.Worksheets("Funky")
    ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
    ActiveSheet.Name = NewName
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •