+ Reply to Thread
Results 1 to 8 of 8

macro for saving multiple worksheets in to a new workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2007
    Posts
    7

    macro for saving multiple worksheets in to a new workbook

    Hi, I cannot get the below to work correctly for me. I am trying to copy two worksheets form a certain workbook in to a new workwork. This new workbook is then saved down every day in the following location T:\InterDepartment\Pricing\Loans\. I want to create a saving code that updates the file in to a corresponding month folder and then eventually a year folder. I keep gettting an error message on the code below ( which is mostly copied an applied from other websites etc)
    Any help would be greatly appreciated
    Sub New_Macro()
    '
    ' New_Macro Macro
    
    'Legend for codes used in the macro
    
    Dim WS As Worksheet
    Dim i As Integer
        
    'Copies the sheets to a new workbook:
        Sheets(Array("Prices", "Markit Compare")).Copy
        
    With ActiveWorkbook
        For Each WS In .Worksheets  'Goes through each worksheet in new workbook
            With WS
                .UsedRange.Value = WS.UsedRange.Value 'Writes the values
                
                i = i + 1   'Adds one to i value
                .Name = "Sheet " & i    'Names the sheet as "Sheet " and i value
            End With
        Next WS
        
        Filename = "All Loan Prices"
        
        
    'Defining new terms that are used in the code
    
    Dim CheminDest, NomDest
       YearFolder = Right(Year(Date), 4)
       MonthFolder = MonthName(Month(Date), True) & " " & Right(Year(Date), 2)
       CheminDest = "T:\InterDepartment\Pricing\Loans\" & YearFolder & "\"
       
       If Len(Dir(CheminDest & MonthFolder, vbDirectory)) = 0 Then
        MkDir CheminDest & MonthFolder
    End If
        
    If Len(Dir(CheminDest & MonthFolder, vbNormal)) <> 0 Then
    ActiveWorkbook.Close SaveChanges:=False
    Exit Sub
    End If
    
    ChDir _
            "T:\InterDepartment\Pricing\Loans\" & YearFolder & "\" & MonthFolder
            
    'Saves the file (text version)
    
    ActiveWorkbook.SaveAs Filename:=Filename & " Text File.xls"
    
    wkbk.Save
    
    Set wkbk = Nothing
    
    MsgBox "Text file has been saved ", vbInformation, "Data backup"
    
    ActiveWorkbook.Close
    
    
    End Sub
    Last edited by arlu1201; 05-08-2012 at 06:13 AM. Reason: Please use code tags in future.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for saving multiple worksheets in to a new workbook

    Should the contents of the sheets be copied to the destination workbook or should the sheets copied entirely (like using a move/copy)?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    05-08-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: macro for saving multiple worksheets in to a new workbook

    Quote Originally Posted by arlu1201 View Post
    Should the contents of the sheets be copied to the destination workbook or should the sheets copied entirely (like using a move/copy)?
    The contents of the sheets should be copied in full.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for saving multiple worksheets in to a new workbook

    Can you give me some more details like -

    1.Name of the source file and path if the file needs to be opened by the macro.
    2. Name of the destination file and path if the file needs to be opened by the macro.
    3. Names of the sheets that need to be copied.
    4. Names of the month & year folders where the file should be saved.

  5. #5
    Registered User
    Join Date
    05-08-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: macro for saving multiple worksheets in to a new workbook

    1. The macro will run on the file I open daily. It will be a template as such which is updated and the final report which is saved only contains the two worksheets.
    2.No file needs to be opened to run the file.
    3.The sheets that need to be copied are Prices and Markit Compare.
    4. I want the completed file with the two sheets - Prices and markit Compare to be saved on the following loaction T:\InterDepartment\Pricing\Loans\2012\May 12, this month folder changing automatically in June and so on.

    Any thing else please ask... I hope that is clear now.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for saving multiple worksheets in to a new workbook

    Try this code
    Option Explicit
    
    Sub copy_sheets()
    Dim foldername As String
    
    Application.DisplayAlerts = False
    
    Workbooks.Add
    ActiveWorkbook.SaveAs ("Daily Report.xlsx")
    
    ThisWorkbook.Worksheets("Prices").Cells.Copy Workbooks("Daily Report.xlsx").Worksheets("Sheet1").Range("A1")
    Workbooks("Daily Report.xlsx").Worksheets("Sheet1").Name = "Prices"
    ThisWorkbook.Worksheets("Markit Compare").Cells.Copy Workbooks("Daily Report.xlsx").Worksheets("Sheet2").Range("A1")
    Workbooks("Daily Report.xlsx").Worksheets("Sheet2").Name = "Markit Compare"
    Workbooks("Daily Report.xlsx").Worksheets("Sheet3").Delete
    
    foldername = Format(Date, "yyyy")
    If Dir("T:\InterDepartment\Pricing\Loans\" & foldername) = "" Then
        MkDir ("T:\InterDepartment\Pricing\Loans\" & foldername)
    End If
        
    If Dir("T:\InterDepartment\Pricing\Loans\" & foldername & "\" & Format(Date, "mmm-yy")) = "" Then
        MkDir ("T:\InterDepartment\Pricing\Loans\" & foldername & "\" & Format(Date, "mmm-yy"))
    End If
    
    Workbooks("Daily Report.xlsx").SaveAs ("T:\InterDepartment\Pricing\Loans\" & foldername & "\" & Format(Date, "mmm-yy")) & "\Daily Report.xlsx"
    
    Application.DisplayAlerts = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro for saving multiple worksheets in to a new workbook

    How about this:
    Option Explicit
    
    Sub ArchivePrices()
    Dim WS As Worksheet, CheminDest As String, fNAME As String
    
    'create directories as needed
        On Error Resume Next
        CheminDest = "T:\InterDepartment\Pricing\Loans\" & Year(Date) & "\"
        MkDir CheminDest
        CheminDest = CheminDest & Format(Date, "MMM YY") & "\"
        MkDir CheminDest
        On Error GoTo 0
        fNAME = "All Loan Prices"
    
    'Copies the sheets to a new workbook:
        Sheets(Array("Prices", "Markit Compare")).Copy
        Sheets("Prices").Name = "Sheet1"
        Sheets("Markit Compare").Name = "Sheet2"
    
        With ActiveWorkbook
            For Each WS In .Worksheets  'Goes through each worksheet in new workbook
                WS.UsedRange.Value = WS.UsedRange.Value         'Writes the values
            Next WS
            
            .SaveAs Filename:=CheminDest & fNAME & " Text File.xls"
            .Close False
        End With
            
        MsgBox "Text file has been saved ", vbInformation, "Data backup"
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  8. #8
    Registered User
    Join Date
    05-08-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: macro for saving multiple worksheets in to a new workbook

    Thanks guys so so much problem is sorted and it is working fine. Thanks again.

+ 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