+ Reply to Thread
Results 1 to 8 of 8

rename sheets according to column A names.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-07-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    244

    rename sheets according to column A names.

    Hi,
    I have a excel file with product names

    I want a macro that will auto generate sheets based on the names of Column A that I select and will name then accordingly.

    I am uploading a sheet for reference. In the example file all my names are in Column A of the tab DATA , and I have all the sheets with the names,
    I want the macro in such a way that I select the names and run the macro and that many sheets will be generate with that name.

    Thanks and regards,
    Me
    Attached Files Attached Files
    Last edited by fatalcore; 08-03-2011 at 03:04 PM.

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: rename sheets according to column A names.

    Hi fatalcore,

    Try the following code and report back. As it is, the code will create a new, blank worksheet and rename it based on the text within the active, selected cell. I don't have Excel 2007 so I couldn't test against your file, but it should work.

    Sub AddSht()
        Dim shtName As String
        Dim sht As Worksheet
        
        shtName = ActiveCell.Value
        
        On Error Resume Next
            Set sht = Sheets(shtName)
            
            If Not sht Is Nothing Then
                MsgBox "Sheet " & shtName & " already exists"
                On Error GoTo 0
            Else
                Worksheets.Add().Name = shtName
            End If
        
    End Sub

  3. #3
    Forum Contributor
    Join Date
    02-07-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: rename sheets according to column A names.

    Hi bigbas,
    The code works ok ...but it only adds one sheet based on selected cell but not cells(as you mentioned in your post).
    can it be a bit tailored made to work on selected CELLS ? That will do my job.
    thanks in advance.

  4. #4
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: rename sheets according to column A names.

    Sorry, I misread. Try the following code and report back.

    Sub AddSht()
        Dim shts As Range
        Dim r As Range
        Dim sht As Worksheet
        
        Set shts = Selection
        
        For Each r In shts
            On Error Resume Next
                Set sht = Sheets(r.Value)
            
                If Not sht Is Nothing Then
                    MsgBox "Sheet " & shtName & " already exists"
                    On Error GoTo 0
                    
                Else
                    Worksheets.Add().Name = r
                End If
        Next r
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    02-07-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: rename sheets according to column A names.

    Hi bigbass.
    It's working great...but it's creating the pages back side..I mean on the left side instead of creating on the right side....It will be great if the pages generate on the right side sequentially.
    thanks in advance.

  6. #6
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: rename sheets according to column A names.

    Hi fatalcore,

    Try this code and report back

    Sub AddSht()
        Dim shts As Range
        Dim r As Range
        Dim sht As Worksheet
        
        Set shts = Selection
        
        For Each r In shts
            On Error Resume Next
                Set sht = Sheets(r.Value)
            
                If Not sht Is Nothing Then
                    MsgBox "Sheet " & shtName & " already exists"
                    On Error GoTo 0
                    
                Else
                    Worksheets.Add(after:=Sheets(Sheets.Count)).Name = r
                End If
        Next r
        
    End Sub

  7. #7
    Forum Contributor
    Join Date
    02-07-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: rename sheets according to column A names.

    Perfect...
    Thanks bro...
    I am marking the trhead as complete...

  8. #8
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: rename sheets according to column A names.

    Not a problem fatal. Glad the solution helped.

+ 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