+ Reply to Thread
Results 1 to 12 of 12

Adapt Copy/Paste Values Routine

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Adapt Copy/Paste Values Routine

    I originally tried to copy selected worksheets containing a mix of pivot tables and formula driven cells from my main workbook to a new workbook and then copy/paste values so that all the information was 'unlinked' from the orignal workbook and the pivot tables were now just tables and no longer pivot tables.

    This all worked well thanks to help from this website, however that approach ran into some problems, so I'm now adopting a different approach. Instead of copying the specified worksheets to a new workbook, I'm simply deleting the unwanted worksheets from the main workbook and resaving the workbook using a different name.

    As a result I'm having a few issues with the code that originally copied the worksheets to the new workbook. Instead of copying and pasting values to the new workbook, I need the code to run on the remaining worksheets in the current workbook but I can't get this to work correctly (despite many hours of trying) and I'd be extremely grateful if anyone can help be adjust the code to achieve this.

    It is the code in red where I'm having the problems. This is (I think) still trying to copy the specified worksheets in the array to a new workbook, but the code needs to run in the current workbook. I hope this makes sense.

    Many thanks

    Sub DeleteSheetsandPasteValues()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim varMySheet As Variant
        Dim pt As PivotTable, arr, rng As Range, i As Long, FName
        
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    
     
        'Make all xlVeryhidden Worksheets visible so they can be removed from the New Workbook
        
        For Each varMySheet In Array("Red", "Green", "Blue", "Yellow", "Orange", "Black", "White", "Pink", "Brown", "Purple")
            Sheets(varMySheet).Visible = xlSheetVisible
        Next varMySheet
    
    
        'Select which Worksheets to keep in the New Workbook, deleting all other Worksheets
        
        For Each ws In ActiveWorkbook.Worksheets
            Select Case ws.Name
                Case "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
                    'Do Nothing
                Case Else
                     ws.Delete
             End Select
        Next ws
        
        
        'Paste values and formats for all Pivot Tables
    
        Set wb = Workbooks.Add
        Set ws = wb.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight")
    
    
        ThisWorkbook.Worksheets(arr).Copy Before:=ws
    
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
            Next pt
        Next i
        
    
        'Paste Values for all non Pivot Table worksheets
    
        For Each ws In ActiveWorkbook.Worksheets
    
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While wb.Worksheets.Count >= i
        wb.Worksheets(i).Delete: Wend
        
        
        'Define which Worksheet is shown
    
        Worksheets("Executive Summary Report").Activate
    
    
        'Saves the Workbook after deleting unwanted Worksheets and Pasting Values and Formats for the remaining Worksheets
        
        ActiveWorkbook.SaveAs Filename:="C:\Reports\My Report (" & Format(DateAdd("m", -1, Now), "mmmm") & ").xlsx", FileFormat:=51
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    Okay, I'm beginnning to make a little progress and have things kind of working, but I'm getting a 'Run-time Error '1004' Copy Method of Range Failed', affecting both ws.cells elements (shown in red below).

    I don't really understand what the error means and why I'm geting it (despite a lot of googling and trying to understand). Can anyone please assist with what I need to do to rectify this problem.

    Many

        'Paste values and formats for all Pivot Tables
    
        Set ws = ActiveWorkbook.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight")
    
        ActiveWorkbook.Worksheets(arr).Copy Before:=ws
    
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
            Next pt
        Next i
        
    
        'Paste Values for all non Pivot Table worksheets
    
        For Each ws In ActiveWorkbook.Worksheets
    
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While ActiveWorkbook.Worksheets.Count >= i
        ActiveWorkbook.Worksheets(i).Delete: Wend
        
        
        'Rename Worksheets
        For Each ws In ActiveWorkbook.Worksheets
            ws.Name = Left$(ws.Name, Len(ws.Name) - 4)
        Next ws
    Last edited by HangMan; 09-09-2015 at 12:31 PM.

  3. #3
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Adapt Copy/Paste Values Routine

    On the cells that you are trying to clear, you should not get a 1004 copy. What is the complete message on that line of code?
    Check and make sure that you don't have protected cells on the sheet.
    When you say cells you are referring to every single cell on the worksheet, you might want to narrow it down. CurrentRegion, UsedRange something besides just cells.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  4. #4
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    Hi skywriter,

    That was my confusion, the error I am getting is 'Run-time Error '1004' - Clear Method of Range Class Failed'

    On the sheets that remain, the ones that I'm looking to copy/paste values and format, there are no protected cells.

    Okay, regarding the range, I've just taken that from the original code which was all working without issue, the range will be different for each individual sheet but I figured as it was pasting values/formats this wouldn't be a problem?

    Re
    ActiveWorkbook.Worksheets(arr).Copy Before:=ws
    The line of code above was all working with no problems in the original code but I've since realised this line is not needed in the updated code, well at least I don't think it is?

    I am very new to VBA and am trying to adapt this by trial and error, so I'm not always 100% sure what the code means but tend to test it line by line to try and understand what it is doing, though not always successfully.
    Last edited by HangMan; 09-10-2015 at 07:38 AM.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Adapt Copy/Paste Values Routine

    There are few errors on the code.

    ' For Each pt In Worksheets(i).PivotTables
    should be


    For Each pt In Worksheets(arr(i)).PivotTables
    Need to use this code to clear data in pivot table.

    Sub test()
    Dim WSD As Worksheet, PVT As PivotTable
      For Each WSD In ActiveWorkbook.Worksheets
    
            For Each PVT In WSD.PivotTables
                PVT.TableRange2.Clear
              
            Next PVT
     Next
    End Sub
    No need to activate a sheet or range.


        For Each ws In ActiveWorkbook.Worksheets
            With ws
            .Cells.Copy
            .[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
           End With
        Next ws
    For Each ws In ActiveWorkbook.Worksheets
            With ws
            .Cells.Copy
            .[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
           End With
        Next ws

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Adapt Copy/Paste Values Routine

    You aren't specifying which element in the array, so I would think this line errors also.

    ActiveWorkbook.Worksheets(arr).Copy Before:=ws

  7. #7
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    Hi AB33,

    Okay, I've tried implementing your suggested changes, but they are throwing up other errors (which I'll detail in another post) and I'm still struggling to understand what the problem is. What I've done is to post the original code below, which all worked really well. This was designed to copy selected worksheets (defined in the array) to a new workbook and then copy and paste values and formats, so there were no actual pivot tables, formulas or links back to the original workbook in the new workbook. These worksheets contained a mixture of pivot tables and formula driven tables and charts.

    Even though this technique worked well there was a problem where it was impossible to get the charts SERIES source to change to the newly created worksheet. As a result what I'm trying to do now (in the second piece of code) is almost the opposite. Rather than copy the specified worksheets to a new workbook, I'm simply deleting the worksheets I don't want in the original workbook but then attempting to copy/paste values and format for the sheets remaining (which are the ones originally copied to the new workbook above), the ones containing the pivot tables, formula driven tables and charts. This way the original problem with the charts SERIES data not changing to the new workbook should vanish.

    I'm sort of assuming part of the problem is that in the original code there were two workbooks, one to copy from the other to copy to, but in my amended version, the code is trying to copy and paste within the same workbook and can't, though I'm not entirely sure. I have tried duplicating the worksheets I want to keep within the same workkbook with a view to trying to copy and paste between the two but again the copy/paste routine still failed.

    Original Working Code
    Sub MyBigReport()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim varMySheet As Variant
        Dim pt As PivotTable, arr, rng As Range, i As Long, FName
        
        Dim oChart As ChartObject
        Dim mySrs As Series
      
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        'Unhide hidden worksheets so they can be copied to a new workbook as part of the array
        For Each varMySheet In Array("Seven", "Eight")
            Sheets(varMySheet).Visible = xlSheetVisible
        Next varMySheet
      
        'Create a new blank workbook and copy selected worksheets to it  
        Set wb = Workbooks.Add
        Set ws = wb.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight")
    
        'Copy Pivot Table Range and Paste Values and Formats to equivalent worksheets in newly created workbook
        ThisWorkbook.Worksheets(arr).Copy Before:=ws
      
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
            Next pt
        Next i
        
        'Copy all other data, e.g. formula driven data and paste values to equivalent worksheets in newly created workbook
        For Each ws In ActiveWorkbook.Worksheets
            
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
      
        While wb.Worksheets.Count >= i
        wb.Worksheets(i).Delete: Wend
        
        Worksheets("Executive Summary Report").Activate
        
        
        'This code was designed to change the SERIES data source for the charts but owing to the nature of the way the charts were created this failed to work
        For Each ws In ActiveWorkbook.Worksheets
            For Each oChart In ws.ChartObjects
                For Each mySrs In oChart.Chart.SeriesCollection
                    mySrs.Formula = WorksheetFunction.Substitute(mySrs.Formula, "MyOriginal Workbook.xlsm", "MyBigReport.xlsx")
                Next
            Next
        Next
        
        
        'Hide worksheets that we don't want to be visible in the final workbook
        For Each varMySheet In Array("Seven", "Eight")
            Sheets(varMySheet).Visible = xlVeryHidden
        Next varMySheet
    
             
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
          
        'Show Save As dialogue window with filename pre-populated, asking the user to save using this name or to provide their own filename. Save the file using a .xlsx format      
        FName = Application.GetSaveAsFilename(InitialFileName:="C:\My Folder\MyBigReport (" & Format(DateAdd("m", -1, Now), "mmmm") & ").xlsx", fileFilter:="Excel workbook (*.xlsx), *.xlsx")
        On Error Resume Next: If FName <> False Then wb.SaveAs FName
        
            
        'Error handler if user saves the file but the filename already exists    
        If Err.Number <> 0 Then
            Application.Dialogs(xlDialogSaveAs).Show
        Err.Clear
            
        End If
        
    End Sub

    My attempt at adapting the code above so it deletes the unwanted worksheets from the current workbook and then Copy/Paste Values and Formats for the remaining worksheets and then saves the file with a new name.

    Revised Code
    Sub MyNewBigReport()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim varMySheet As Variant
        Dim pt As PivotTable, arr, rng As Range, i As Long, FName
        
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
        'Make all xlVeryhidden Worksheets visible so unwanted worksheets can be removed from the Workbook
        
        For Each varMySheet In Array("Red", "Green", "Blue", "Yellow", "Orange", "Black", "White", "Pink", "Seven", "Eight")
            Sheets(varMySheet).Visible = xlSheetVisible
        Next varMySheet
    
    
        'Select which Worksheets to keep in the New Workbook, deleting all other Worksheets
        
        For Each ws In ActiveWorkbook.Worksheets
            Select Case ws.Name
                Case "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
                    'Do Nothing
                Case Else
                     ws.Delete
             End Select
        Next ws
        
        
        'Copy and Paste values and formats for all Pivot Tables
    
        Set ws = ActiveWorkbook.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight")
    
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
               rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
            Next pt
        Next i
    
        'Copy and Paste Values for all non Pivot Table worksheets
    
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            ws.Range("A1").PasteSpecial (xlPasteValues)
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
            
        End With
            
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While ActiveWorkbook.Worksheets.Count >= i
        ActiveWorkbook.Worksheets(i).Delete: Wend
        
          
        'Define which Worksheet is shown by default when the file is opened
    
        Worksheets("One").Activate
    
    
        'Saves the Workbook with a new name
        
        ActiveWorkbook.SaveAs Filename:="C:\MyFolder\MyNewBigReport Report (" & Format(DateAdd("m", -1, Now), "mmmm") & ").xlsx", FileFormat:=51
    
        
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    
    End Sub
    Last edited by HangMan; 09-10-2015 at 08:24 AM.

  8. #8
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    Hi AB33,

    Coming back to you re your suggested code changes...

    This line doesn't appear to be causing any problems in as much as it isn't tripping the code up and both seem to work, but happy to use the correct code.

    For Each pt In Worksheets(i).PivotTables
    neither did changing it to this

    For Each pt In Worksheets(arr(i)).PivotTables

    I tried changing the line below in red with the one you suggested (in blue), but this generates a 'Run-time Error '1004' - Unable to get the TableRange2 Property of the PivotTable Class, so I'm unsure if I have not done this correctly?

    Sub MyNewBigReport()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim varMySheet As Variant
        Dim pt As PivotTable, arr, rng As Range, i As Long, FName
    
    
        Set ws = ActiveWorkbook.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight")
    
        For Each ws In ActiveWorkbook.Worksheets
    
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                'ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                pt.TableRange2.Clear
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
               rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
            Next pt
        Next i

    I also tried changing this

        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.Range("A1").PasteSpecial (xlPasteValues)
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While ActiveWorkbook.Worksheets.Count >= i
        ActiveWorkbook.Worksheets(i).Delete: Wend
    to this

        For Each ws In ActiveWorkbook.Worksheets
            With ws
            .Cells.Copy
            .[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
           End With
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While ActiveWorkbook.Worksheets.Count >= i
        ActiveWorkbook.Worksheets(i).Delete: Wend
    as per your suggestion (although maybe again I'm doing something wrong), but I am getting the same 'Run-time '1004' - Clear Method of Range Class Failed' error.

    This is where I am now completly stuck with the code. I'm sure I'm missing something simple and obvious but any help would be gratefully appreciated to get this running correctly.

    Many thanks
    Last edited by HangMan; 09-10-2015 at 07:32 AM.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Adapt Copy/Paste Values Routine

    These 2 are not the same

    For Each pt In Worksheets(arr(i)).PivotTables ' looping through the array sheet
    and

    For Each pt In Worksheets(i).PivotTables ' looping through the sheets using an index no.

    Why do you need to complicate by using an array and looping through the array while a simple line could do the job?
     For Each WSD In ActiveWorkbook.Worksheets(Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"))
    Sub test()
    'arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight")
    Dim WSD As Worksheet, PVT As PivotTable
      For Each WSD In ActiveWorkbook.Worksheets(Array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"))
       'For Each WSD In ActiveWorkbook.Worksheets(Array("One", "Two", "Three"))
    
            For Each PVT In WSD.PivotTables
                PVT.TableRange2.Clear
              
            Next PVT
     Next
    End Sub

  10. #10
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    Happy to adopt whatever works for this. My main problem is how to 'fix' the main code to get it to work properly. I have very limited VBA knowledge so am happy to be advised as to the best approach.

  11. #11
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    Hi AB33,

    I'm still getting nowhere with the code. I take on board what you are saying but my VBA knowledge is still lacking and I don't know which is the best approach to certain tasks and wheter or not an array is better to use or not.

    This code creates a new workbook and copies seven worksheets from my original workbook to the new workbook and then copies and pastes the values and formats for the pivot tables on those sheets where it finds them and pastes values for the other sheets and works perfectly.

    Sub ExecutiveSummaryReportExport()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim varMySheet As Variant
        Dim pt As PivotTable, arr, rng As Range, i As Long, FName
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        Set wb = Workbooks.Add 
        
        Set ws = wb.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven")
    
      
        ThisWorkbook.Worksheets(arr).Copy Before:=ws  '<== Copies the worksheets listed in the array to the new workbook, placing the sheets before Sheet1, Sheet2 and Sheet3
    
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
               rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
            Next pt
        Next i
    
    
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While wb.Worksheets.Count >= i
        wb.Worksheets(i).Delete: Wend
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    End Sub
    What I want to be able to do is to adapt the code above so that instead of creating a new workbook and copying the seven sheets to the new workbook, it instead deletes all worksheets in my original workbook apart from the seven and then copies and paste values and format for the pivot tables on those sheets within my original workbook.

    I can manage to delete the worksheets I need, leaving me with just my seven sheets but the problem I keep coming up against is the copy/paste values and formats. I've tried implementing the code you suggested but I am still getting an error when trying to clear and copy the cells, with both 'ws.Cells.Clear' and 'ws.Cells.Copy'. I also get the same error using your code with 'pt.TableRange2.Clear' so I'm completely stuck and wondered whether you can show me where I am going wrong because I don't understand how to resolve the problem. I tried both an array and your non srray approach but still just get errors. I don't understand why when moving the sheets to a new workbook this all works but when trying to achieve the same thing in the original workbook it errors.

    Sub Big()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim varMySheet As Variant
        Dim pt As PivotTable, arr, rng As Range, i As Long, FName
        
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
    
        'Make all xlVeryhidden Worksheets visible so they can be removed from the New Workbook
        
        For Each varMySheet In Array("Red", "Green", "Blue", "Yellow", "Orange", "Black", "White", "Pink", "Six", "Seven")
            Sheets(varMySheet).Visible = xlSheetVisible
        Next varMySheet
    
    
        'Select which Worksheets to keep in the New Workbook, deleting all other Worksheets
        
        For Each ws In ActiveWorkbook.Worksheets
            Select Case ws.Name
                Case "One", "Two", "Three", "Four", "Five", "Six", "Seven"
                    'Do Nothing
                Case Else
                     ws.Delete
             End Select
        Next ws
        
        
        'Create a blank worksheet at the end of the workbook
        
        With ThisWorkbook
            Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        End With
        
        
        'Define which Worksheet is shown by default
    
        Worksheets("Executive Summary Report").Activate
        
        Set ws = ActiveWorkbook.Worksheets(1): arr = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven")
    
    
        'Copy and Paste Values and Formats for all Pivot Tables
    
        For i = 1 To UBound(arr) + 1
            For Each pt In Worksheets(i).PivotTables
                ws.Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                ws.Range("A1").PasteSpecial (xlPasteValues)
                ws.Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: ws.Range("A1").CurrentRegion.Copy rng
               rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
            Next pt
        Next i
    
        'Paste Values for all non Pivot Table worksheets
        
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.Range("A1").PasteSpecial (xlPasteValues)
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
            Next ws
        Cells(1, 1).Select
    
      i = UBound(arr) + 2
    
        While ActiveWorkbook.Worksheets.Count >= i
        ActiveWorkbook.Worksheets(i).Delete: Wend
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    End Sub
    Many thanks for your help...

  12. #12
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Adapt Copy/Paste Values Routine

    I think I'm finally making some progress...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Adapt Code to copy and paste multiple Ws to multiples Ws to another Wb
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-13-2015, 11:42 AM
  2. How to adapt this code to copy unique values in cells
    By RayJay01 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-02-2014, 07:18 AM
  3. Select case logical range copy/paste routine
    By coasterman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-17-2013, 01:10 PM
  4. Replies: 1
    Last Post: 09-04-2012, 04:19 PM
  5. [SOLVED] Adapt code to copy and paste
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2012, 07:52 PM
  6. Excel VBA - Problem programming routine for conditional copy/paste
    By CydMM in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-31-2012, 07:50 AM
  7. help w/ generic copy & paste/paste special routine
    By DavidH in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-23-2006, 12:00 AM

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