+ Reply to Thread
Results 1 to 8 of 8

Copy All Sheets and Paste Values To New Workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Copy All Sheets and Paste Values To New Workbook

    Hello All,

    I think this should be easy, but I have scoured the internet and not found a workable solution. I have a worksheet named Test that I need to copy all sheets to a new worksheet called Final and paste Values only.

    I will use this on 6 or 7 different worksheets with different numbers of sheets and different names.

    I thought this would be super easy, but so far I have not found a workable solution. Any help is greatly appreciated.

    Kelly
    Last edited by jkelly228; 04-29-2011 at 04:23 PM.

  2. #2
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Copy All Sheets and Paste Values To New Workbook

    Maybe something along these lines?

    Adjust to your needs... Thank You , Mike

    From VBA Express...



    Sub CopyFromWorksheets() 
        Dim wrk As Workbook 'Workbook object - Always good to work with object variables
        Dim sht As Worksheet 'Object for handling worksheets in loop
        Dim trg As Worksheet 'Master Worksheet
        Dim rng As Range 'Range object
        Dim colCount As Integer 'Column count in tables in the worksheets
         
        Set wrk = ActiveWorkbook 'Working in active workbook
         
        For Each sht In wrk.Worksheets 
            If sht.Name = "Master" Then 
                MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _ 
                "Please remove or rename this worksheet since 'Master' would be" & _ 
                "the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error" 
                Exit Sub 
            End If 
        Next sht 
         
         'We don't want screen updating
        Application.ScreenUpdating = False 
         
         'Add new worksheet as the last worksheet
        Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count)) 
         'Rename the new worksheet
        trg.Name = "Master" 
         'Get column headers from the first worksheet
         'Column count first
        Set sht = wrk.Worksheets(1) 
        colCount = sht.Cells(1, 255).End(xlToLeft).Column 
         'Now retrieve headers, no copy&paste needed
        With trg.Cells(1, 1).Resize(1, colCount) 
            .Value = sht.Cells(1, 1).Resize(1, colCount).Value 
             'Set font as bold
            .Font.Bold = True 
        End With 
         
         'We can start loop
        For Each sht In wrk.Worksheets 
             'If worksheet in loop is the last one, stop execution (it is Master worksheet)
            If sht.Index = wrk.Worksheets.Count Then 
                Exit For 
            End If 
             'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
            Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount)) 
             'Put data into the Master worksheet
            trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value 
        Next sht 
         'Fit the columns in Master worksheet
        trg.Columns.AutoFit 
         
         'Screen updating should be activated
        Application.ScreenUpdating = True 
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Copy All Sheets and Paste Values To New Workbook

    Hi RealNiceGuy5000,

    Thank you for the help. However, I think I was not clear. All of these sheets are actually in 1 workbook, and I need to move them to a 2nd workbook.

    Workbook 1 = Test
    Workbook 2 = Final

    Sorry for the confusion.

  4. #4
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Copy All Sheets and Paste Values To New Workbook

    I have found the below code, and I can get it to work to copy all sheets to a new workbook, but I want it to copy only values and formats, not formulas.

    Any help is appreciated. Thanks

    Kelly

    Sub Run_This()
    
    
    
    '    Application.ScreenUpdating = False
    '    Application.EnableEvents = False
    '    Application.DisplayAlerts = False
        
    
        Dim wb1 As Workbook
        Dim wb2 As Workbook
        Set wb1 = ActiveWorkbook
        Set wb2 = Workbooks("Demand Plan Review_APAC.xlsx")
        For Each Sheet In wb1.Sheets
            If Sheet.Visible = True Then
                'copy the sheets after the last
                'sheet of the destination workbook
                Sheet.Copy After:=wb2.Sheets(wb2.Sheets.Count)
            End If
        Next Sheet
     
    End Sub

  5. #5
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Copy All Sheets and Paste Values To New Workbook

    Maybe something like this?

    Thank You, Mike


    For Each Sheet In wb1.Sheets
            If Sheet.Visible = True Then
                'copy the sheets after the last
                'sheet of the destination workbook
                Sheet.Copy After:=wb2.Sheets(wb2.Sheets.Count)
            End If
        Cells.Copy
        ActiveSheet.Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveSheet.Range("A1").Select
           Application.CutCopyMode = False       
        Next Sheet

  6. #6
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Copy All Sheets and Paste Values To New Workbook

    Hi RealNiceGuy5000,

    That worked perfectly. Thank you very much.

    Kelly

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy All Sheets and Paste Values To New Workbook

    Why copying ?

    sub snb()
      for each sh in sheets
        sh.usedrange=sh.usedrange.value
      next
      thisworkbook.saveas "E:\OF\a copy of the originalworkbook.xls"
    End sub



  8. #8
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Copy All Sheets and Paste Values To New Workbook

    Quote Originally Posted by jkelly228 View Post
    Hi RealNiceGuy5000,

    That worked perfectly. Thank you very much.

    Kelly
    Glad to be of some help.

    .

    Thank You, Mike

+ 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