+ Reply to Thread
Results 1 to 9 of 9

Copy sheets to new file and rename it

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-19-2006
    Posts
    113

    Copy sheets to new file and rename it

    Hi everybody,
    Without experience in programming I need help from you, everyone who could help me.
    I have 50 worksheets in 1 workbook. Any of those sheets I named XXREC – XX is a number (01REC, 02REC, 03REC,… 50REC). I need to create a Workbook for each worksheet, rename the workbook with the actual name (I mean, number) of the sheet without the 3 last letters (REC) and rename the sheet inside the workbook, with Sheet1.

    Until now, checking other examples, I create this:

    Sub CreateBooks()
        Workbooks.Open Filename:="C:\exp1.xls"
    Dim w As Worksheet
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
            For Each w In ActiveWorkbook.Worksheets
                w.Copy
                    ActiveWorkbook.SaveAs Filename:="C:\Exp\" & w.Name
                        ActiveWorkbook.Close
        Next w
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
                        ActiveWorkbook.Close
    End Sub
    Thank you in advance for any help
    Last edited by VBA Noob; 06-26-2007 at 01:39 PM.

  2. #2
    Registered User
    Join Date
    01-09-2007
    Posts
    59
    Please wrap code next time


    Try this:

    Sub CreateBooks()
    
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim sht As Worksheet
    Dim sht2 As Worksheet
    Dim fPath As String
    
    Workbooks.Open fileName:="C:\exp1.xls"
    
    Set wb1 = ActiveWorkbook
    
    fPath = Left(wb1.FullName, Application.WorksheetFunction.Find(wb1.Name, wb1.FullName) - 1)
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    For Each sht In wb1.Sheets
        Workbooks.Add
        Set wb2 = ActiveWorkbook
        sht.Copy Before:=wb2.Sheets(1)
        For Each sht2 In wb2.Sheets
            If sht2.Name <> sht.Name Then
                sht2.Delete
            End If
        Next sht2
        wb2.Sheets(sht.Name).Name = "Sheet1"
        wb2.SaveAs fileName:=fPath & Left(sht.Name, Len(sht.Name) - 3)
    Next sht
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    End Sub
    ska
    Last edited by VBA Noob; 06-26-2007 at 01:40 PM.

  3. #3
    Forum Contributor
    Join Date
    12-19-2006
    Posts
    113

    Thk U Ska67Can

    Hi Ska67Can
    Here (Asia) it's too late (01:50) to check anything...
    Tomorrow, back to my office, I'll do it and I'll tell you something.
    Anyway, thank you for your reply.
    Regards
    PS-Do you like ska music?

  4. #4
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Jokacave,

    Here is the code you were using with a few minor modifications and it appears to do what you are wanting it to do on my end.

    Sub CreateBooks()
    
    Dim w As Worksheet
    Dim sName As String
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each w In ActiveWorkbook.Worksheets
        w.Copy
        sName = w.Name
        Sheets(sName).Name = "Sheet1"
        ActiveWorkbook.SaveAs Filename:="C:\Exp" & Left(w.Name, 2)
        ActiveWorkbook.Close
    Next w
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    ActiveWorkbook.Close
    
    End Sub
    Sincerely,
    Jeff

  5. #5
    Forum Contributor
    Join Date
    12-19-2006
    Posts
    113

    Thk U boylejob

    Hi Jeff,
    As I told to Ska67can it's too late here to check anything but tomorrow I'll tell you something.
    Anyway, my thanks to you too.
    Regards

  6. #6
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Jokacave,

    Great! I look forward to hearing how both sets of code work. I am sure that both with do what you are wanting. I just wanted to give you something as close to what you had already written as possible.

    Where in Asia are you?

  7. #7
    Forum Contributor
    Join Date
    12-19-2006
    Posts
    113

    Smile

    Jeff,

    Macau - casinoland!!! - ex-portuguese colony, part of PR China, and like Hong Kong, autonomous region of China.

    We talk tomorrow. Sorry.

    Regards

+ 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