Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: Renaming Sheets with Vba

  1. #1
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    i have some code for renaming sheets which works fine until you try to do the same thing twice in a row for eg. Below


    Sheets("111stk").Select
    ActiveWindow.SelectedSheets.Delete
    Sheets.Add
    Sheets("Sheet1").Name = "111stk"
    Sheets("211stk").Select
    ActiveWindow.SelectedSheets.Delete
    Sheets.Add
    Sheets("Sheet2").Name = "211stk"
    Range("G34").Select
    Sheets("511stk").Select
    ActiveWindow.SelectedSheets.Delete
    Sheets.Add
    Sheets("Sheet3").Name = "511stk"

    has anyone got any better code so I don't have to refer to "Sheet3" and so on

    HELp?

  2. #2
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi BrettVBA

    Looking @ your current code I would say that
    you are currenly adding new sheets over the old ones
    ie. deleting some currently setup sheets
    then adding new ones Named as the same.
    What you have found out is that the sheets
    counter increments in the name. So that referencing the sheet as sheet3 etc is not valid after a 2nd call.
    If your sheets names are cost as you have set up then
    try this



    Option Explicit
    Option Base 1

    Sub AddSheets()
    Dim aSheets()
    Dim i As Integer

    aSheets = Array("111stk", "211stk", "511stk")

    Application.DisplayAlerts = False
    On Error GoTo ErrAdd
    For i = 1 To 3
    Sheets(aSheets(i)).Delete
    Sheets.Add.Name() = aSheets(i)
    Next

    Exit Sub

    ErrAdd:
    MsgBox Err.Number & ":=" & Err.Description, vbSystemModal + vbMsgBoxHelpButton, _
    "Error Adding Sheet", Err.HelpFile, Err.HelpContext

    End Sub



    Kind Regards,
    Ivan F Moala From the City of Sails

  3. #3
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default


  4. #4
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    thats exactually what I needed thanks Ivan

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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