Consulting

Results 1 to 8 of 8

Thread: Solved: Work Sheet Naming

  1. #1
    VBAX Regular
    Joined
    Apr 2007
    Posts
    20
    Location

    Solved: Work Sheet Naming

    Edit Lucas: Cross posted.

    Click here for an explanation of cross-posting

    I have a macro that keeps on adding work sheet.I need the sheet to be named say " Ex 1" whenever a sheet is added. Is there a way to do that.. if yes how should I go about that??

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    In your macro, after the command to create the sheet, name it, something like this:

    [VBA]
    ActiveWorkbook.Sheets.Add
    ActiveSheet.Name = "Ex 1"
    [/VBA]

    If you want you can put that into a loop to name multiple sheets. If you post your macro will be a little easier to customize it for you.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  3. #3
    VBAX Regular
    Joined
    Apr 2007
    Posts
    20
    Location
    Thanks... it was very helpful... thanks a ton

  4. #4
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Don't forget to mark the thread as "Solved" if you're happy with the result - you can do this via the Thread Tools at the top of the page.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add this to your ThisWorkbook module to increment sheet names as added.
    [vba]Option Explicit
    Private Sub Workbook_NewSheet(ByVal sh As Object)
    ExSheet sh
    End Sub

    Sub ExSheet(sh As Worksheet)
    Dim Nm1 As Long, Nm2 As Long, Sht As Worksheet
    Nm1 = 0
    For Each Sht In Worksheets
    If Left(Sht.Name, 2) = "Ex" Then
    Nm2 = Split(Sht.Name)(1)
    If Nm2 > Nm1 Then
    Nm1 = Nm2
    End If
    End If
    Next
    If Nm1 = 0 Then
    sh.Name = "Ex 1"
    Else
    sh.Name = "Ex " & Nm1 + 1
    End If
    End Sub
    [/vba]
    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'

  6. #6
    VBAX Mentor Brandtrock's Avatar
    Joined
    Jun 2004
    Location
    Titonka, IA
    Posts
    399
    Location
    Cross posted HERE
    Brandtrock




  7. #7
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    He's playing with fire. I don't like this behavior . I think I will put a yellow sticky note on my screen with his username.

    Charlize

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Edit Lucas: Cross posted.

    Click here for an explanation of cross-posting

    Thanks Brandtrock....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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