+ Reply to Thread
Results 1 to 3 of 3

Rename worksheet name "Sheet1" in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2003
    Posts
    7

    Question Rename worksheet name "Sheet1" in VBA

    How to rename the worksheet name "Sheet1" to "File1" in VBA?
    My VBA coding as follows:
    Sub Copyfile()
    FileCopy ("C:\Data2006.xls"), C:\Book1.xls
    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Change Sheet Names

    here are four codes you can use

    Sub Macro2()
    '
    ActiveSheet.Name = "File1"
    End Sub
    Sub Macro3()
    '
    ActiveSheet.Name = Range("a1")
    End Sub
    Sub Macro6()
    '
    Sheets(1).Name = "File1"
    End Sub
    Sub Macro7()
    '
    Sheets(1).Name = Sheets(2).Range("A1")
    End Sub
    Last edited by davesexcel; 11-29-2006 at 03:33 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: Rename worksheet name "Sheet1" in VBA

    Hi

    If you use Workbooks.Add 1 then you will create a new workbook with just 1 sheet, which you can then delete at the end.
    Maybe something like

    Private Sub CommandButton1_Click()
        Dim wbNew As Workbook
        Dim src As Workbook
        Dim sh As Worksheet
        Dim myarray As Variant
        Application.DisplayAlerts = False
        Workbooks.Add 1
        Set wbNew = ActiveWorkbook
        Set src = ThisWorkbook
        For Each sh In src.Worksheets(Array("dataExtract", "pt"))
            sh.Activate
            myarray = sh.UsedRange.Value
            wbNew.Sheets.Add.Name = sh.Name
            wbNew.Sheets(sh.Name).Range("A1").Resize(UBound(myarray), UBound(myarray, 2)) = myarray
        Next sh
        wbNew.Sheets(1).Delete
    
        wbNew.SaveAs Filename:="something.xls"
        wbNew.Close
        Application.DisplayAlerts = True
    End Sub
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

+ 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