Consulting

Results 1 to 16 of 16

Thread: Rename sheets

  1. #1

    Arrow Rename sheets

    I want to import all the sheets in my workbook_2 of workbook_1.

    Sheet names of workbook_1 are always different.

    After importing all the sheets from workbook_1, rename all the imported sheets with its name appending with "1" (for eg; MySheet -> MySheet1 and so on)

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi ilyaskazi,

    Put the two books in a folder, paste this code in the book where you want the sheets imported to and run the code (Note: this is not restricted to importing sheets from one book - it will import all the sheets from all the books that you put in the folder).

    Note that if you have any links in your worksheets, those links will now point to the original workbook[vba]Option Explicit

    Sub AmalgamateBooks()
    Dim ThisBook As Workbook, OtherBook As Workbook, SheetName$, i&, N&
    Set ThisBook = ActiveWorkbook
    Application.ScreenUpdating = False
    With Application.FileSearch
    .LookIn = ActiveWorkbook.Path
    .FileName = "*.xls"
    If .Execute > 0 Then
    For i = 1 To .FoundFiles.Count
    If .FoundFiles(i) <> ThisWorkbook.FullName Then
    Application.Workbooks.Open(.FoundFiles(i)).Activate
    Set OtherBook = ActiveWorkbook
    With OtherBook
    For N = 1 To Sheets.Count
    Sheets(N).Activate
    SheetName = ActiveSheet.Name
    Cells.Copy
    With ThisBook
    .Activate
    Worksheets.Add After:=Sheets(Sheets.Count)
    With .ActiveSheet
    .Paste
    .Name = SheetName & "1"
    [A1].Select
    End With
    Application.CutCopyMode = False
    End With
    .Activate
    Next N
    End With
    With OtherBook
    .Close False
    End With
    End If
    Next i
    End If
    End With
    Sheet1.Activate
    End Sub
    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    thankyou john,

    i m checking your codes. after renaming with digit as "1", these sheets hv to rename with its original name after completion of its work in this workbook. Means remove "1" from all the sheets name.

    Here different execution function required.

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi,

    When you need to remove the "1", just use
    ActiveSheet.Name = Left(ActiveSheet.Name, Len(ActiveSheet.Name) - 1)
    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5

    Arrow

    Suppose I do not want to import any sheets from any excel file
    but I need to rename all sheets containing in my activeworkbook.

    for eg.: "PRTP" => "PRTP1", "CHET" => "CHET1", "ILYS" => "ILYS1"

    Workbook may contain sheets in any numbers with any names.

    I hv tried solving like this...

    Sub Rename_Sheet1()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
    ws.Name = ws.Name & "1"
    Next ws
    End Sub
    ...but no output came.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That code worked fine for me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Try this variation

    Option Explicit
    
    Sub Rename_Sheet1()
        Dim ws As Worksheet
    For Each ws In Worksheets
            ws.Name = ws.Name & "1"
        Next ws
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    Then why i m not geting the output???

    I m using office xp-2003. Does it matter?

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by ilyaskazi
    Then why i m not geting the output???

    I m using office xp-2003. Does it matter?
    2003 should make no difference - both versions of the code should work
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  10. #10
    ok now its working correct.

    To remove "1" from all worksheets, this code does not work..

    ActiveSheet.Name = Left(ActiveSheet.Name, Len(ActiveSheet.Name) - 1)
    how to rename all???

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by ilyaskazi
    this code does not work..

    ActiveSheet.Name = Left(ActiveSheet.Name, Len(ActiveSheet.Name) - 1)
    Afraid to say, but it works for me.

    You might want to make it generic to cater for Sheet11 etc.


    Dim sNum As String
    Dim iPos As Long
    Dim i As Long
        With ActiveSheet
            For i = Len(.Name) To 1 Step -1
                If Not IsNumeric(Mid(.Name, i, 1)) Then
                    iPos = i
                    Exit For
                End If
            Next i
            If iPos <> Len(.Name) Then
                .Name = Left(ActiveSheet.Name, iPos)
            End If
        End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    hth,

    SHEET NAMES: "ILYS1", "RAJ2", "KANT1", "FRDSIS2"

    I want to remove last digit "1" only and not "2" from all the worksheets in the workbook.

    Also plz let me know whether it works perfectly for startup file "PERSONAL.xls". My macro is in this file only and not in any other workbook.

    -----------------------------------------x--------x--------------------------------

    next question: rename all worksheets with its orginal name appending with "1"
    -------------

    renaming with "1" works only with the workbook for our file.
    i m writing code in my "PERSONAL.xls" --the startup file of excel
    Try this and let me know whether now it works??

    i hv checked here, and the only reason i found unable to excute this code.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by ilyaskazi
    SHEET NAMES: "ILYS1", "RAJ2", "KANT1", "FRDSIS2"

    I want to remove last digit "1" only and not "2" from all the worksheets in the workbook.

    With ActiveSheet
            If Right(.Name, 1) = "1" Then
                .Name = Left(.Name, Len(.Name) - 1)
            End If
        End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    singly perfect, but how to do if to rename for all the worksheets if found??

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by ilyaskazi
    singly perfect, but how to do if to rename for all the worksheets if found??

    For Each sh In ActiveWorkbook.Worksheets
        With sh
            If Right(.Name, 1) = "1" Then
                .Name = Left(.Name, Len(.Name) - 1)
            End If
        End With
    Next sh




  16. #16
    dat's good and totally perfect now. Everything is solved.

    Thankyou very much for your precious time.

    regards,
    ilyas kazi

Posting Permissions

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