+ Reply to Thread
Results 1 to 16 of 16

Copy/Paste Clipboard in VBA

Hybrid View

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

    Copy/Paste Clipboard in VBA

    The attached Workbook which contains a sheet with two pivot tables. I want to convert the pivot tables into normal tables, so the source data is no longer linked. I'm using this technique manually but want to convert it into a macro:
    1. Select the pivot table cells and press Ctrl+C to copy the range.
    2. Use Paste Special to Paste Values.
    3. Display the Office Clipboard.
    4. With the unlinked pivot selected, click the item on the Office Clipboard that corresponds to the pivot table copy operation. It will be the last item, unless you copied something else.

    I went through the steps and recorded the actions to give me this:

    Sub PasteClipboard()
    
        Range("B4:F9").Select
        Selection.Copy
        Range("B4").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        ActiveSheet.Paste
    End Sub
    However if I then attempt to run the macro on my original pivot tables I get and error (highlighted in red in the code above). Is there anyway to resolve this?

    Many thanks
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Copy/Paste Clipboard in VBA

    Hello HangMan,

    You can copy the values without using Copy/Paste.

    Sub TestMacro()
    
        Dim Data    As Variant
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Worksheets("Pivot")
            
            Set Rng = Wks.Range("B4").CurrentRegion
            Data = Rng.Value
            Rng.ClearContents
            Rng.Value = Data
            
            Set Rng = Wks.Range("B13").CurrentRegion
            Data = Rng.Value
            Rng.ClearContents
            Rng.Value = Data
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: Copy/Paste Clipboard in VBA

    Hi Leith,

    Okay, though the element missing is the 'Paste Format' from the clipboard, step 4 on the orignal post, this puts the original formatting back onto the table of pasted values. This is what I would like to try and achieve through the macro if it is possible?

    Many thanks

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

    Re: Copy/Paste Clipboard in VBA

    I've constructed this code from various sources which copies specified worksheets from a Workbook to a new Workbook using Paste Values. The only element I can't figure out is how to paste the pivot table formats. This is my code:

    Sub PasteValues()
    
        Dim ws As Worksheet
        Dim wb As Workbook, wbNew As Workbook
             
        With Application
            .ScreenUpdating = False
                                       
            Set wb = ThisWorkbook
                wb.Worksheets(Array("Pivot", "Blue", "Green")).Copy
            Set wbNew = ActiveWorkbook
                     
            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
                      
            .ScreenUpdating = True
        End With
        
        wbNew.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx"
         
    End Sub
    I've tried adding the code in red but I can't seem to get it to paste the pivot table format, i.e. Paste Clipboard, can anyone please assist?

    Sub PasteValues()
    
        Dim ws As Worksheet
        Dim wb As Workbook, wbNew As Workbook
        
        Dim pt As PivotTable
        Dim NextRow As Range
        Set pt = ActiveSheet.PivotTables(1)
    
              
        With Application
            .ScreenUpdating = False
                                       
            Set wb = ThisWorkbook
                wb.Worksheets(Array("Pivot", "Blue", "Green")).Copy
            Set wbNew = ActiveWorkbook
                     
            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
            
            Set NextRow = ws.Cells(Cells.Rows.Count).End(xlUp).Offset(1)
                For Each pt In ws.PivotTables
                pt.TableRange2.Copy
                Set CurrentRow = NextRow
                CurrentRow.Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Application.CutCopyMode = False
                CurrentRow.Select
                Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Application.CutCopyMode = False
            Next pt
                      
            .ScreenUpdating = True
        End With
        
        wbNew.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx"
         
    End Sub
    Many thanks

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy/Paste Clipboard in VBA

    Maybe :

    Sub Test()
      Dim pt As PivotTable, rng As Range
      With Worksheets.Add
        For Each pt In Sheets("Pivot").PivotTables
          .Cells.Clear: Set rng = pt.TableRange2: rng.Copy
          .Range("A1").PasteSpecial (xlPasteValues)
          .Range("A1").PasteSpecial (xlPasteFormats)
          rng.Clear: .Range("A1").CurrentRegion.Copy rng
          rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
        Next pt
        Application.DisplayAlerts = False: .Delete: Application.DisplayAlerts = True
      End With
    End Sub

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

    Re: Copy/Paste Clipboard in VBA

    Hi karedog,

    How would I amalgamate this with my main piece of code, which is copying other worksheets to the new workbook. Sorry, I'm still very new to VBA?

    Many thanks

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

    Re: Copy/Paste Clipboard in VBA

    I've attempted to integrate your code with my code but I can't get the paste formats for the pivots to work, even though it works with your standalone code, what am I doing wrong?

    Sub PasteValues()
    
        Dim ws As Worksheet
        Dim wb As Workbook, wbNew As Workbook
        Dim pt As PivotTable, rng As Range
      
        With Application
            .ScreenUpdating = False
                                       
            Set wb = ThisWorkbook
                wb.Worksheets(Array("Pivot", "Blue", "Green")).Copy
            Set wbNew = ActiveWorkbook
                     
            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
      
            For Each pt In Sheets("Pivot").PivotTables
                .Cells.Clear: Set rng = pt.TableRange2: rng.Copy
                .Range("A1").PasteSpecial (xlPasteValues)
                .Range("A1").PasteSpecial (xlPasteFormats)
                rng.Clear: .Range("A1").CurrentRegion.Copy rng
                rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
            Next pt 
                      
            .ScreenUpdating = True
        End With
        
        wbNew.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx"
         
    End Sub

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy/Paste Clipboard in VBA

    Quote Originally Posted by HangMan View Post
    Hi karedog,
    .... Sorry, I'm still very new to VBA?
    Many thanks
    You are welcome HangMan.
    Me too

    Sub Test2()
      Dim wb As Workbook, ws As Worksheet, pt As PivotTable, arr, rng As Range, i As Long
      Application.ScreenUpdating = False: Application.DisplayAlerts = False
      Set wb = Workbooks.Add: Set ws = wb.Worksheets(1): arr = Array("Pivot", "Red", "Green")
      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
          rng.Parent.ListObjects.Add xlSrcRange, rng, , xlYes
        Next pt
      Next i
      i = UBound(arr) + 2: While wb.Worksheets.Count >= i: wb.Worksheets(i).Delete: Wend
      wb.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx": wb.Close
      Application.ScreenUpdating = True: Application.DisplayAlerts = True
    End Sub

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

    Re: Copy/Paste Clipboard in VBA

    Okay, I think I've figured out the PasteValue for the other sheets by adding a bit of code back in... but please let me know if this is not a good way to do this? And also figured out how to lose the dropdown arrows and the SaveAs... so beginning to get there...

    Just trying to figure out the file saving part... Is this possible?

    Sub Test2()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim pt As PivotTable, arr, rng As Range, i As Long
      
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
      
        Set wb = Workbooks.Add
        Set ws = wb.Worksheets(1): arr = Array("Pivot", "Red", "Green")
      
        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
    '           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.GetSaveAsFilename
        
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        
    End Sub
    Last edited by HangMan; 09-04-2015 at 06:12 AM.

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

    Re: Copy/Paste Clipboard in VBA

    Hi Karedog,

    Thanks for this, looking good. There are couple of things, the sheets not containing pivot tables need to be copied using PasteValues and PasteFormats, so there are no formula links to the original sheet. This is what I was struggling with integrating your code with my code.

    On the pivots I notice they has dropdown arrows on each column header, is it possoble to remove these completely?

    Finally, if I run the code and the file is saved to the specified location and then I run the code again it doesn't warn me that there is already a file with that name and ask if I want to overwrite it, which is baffling me? I think it would actually be far better if it could come up with a dialogue asking the user to enter a filename for the file and select the location if that is possible?

    Many thanks
    Last edited by HangMan; 09-04-2015 at 05:31 AM.

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy/Paste Clipboard in VBA

    Maybe :

    Sub Test3()
      Dim wb As Workbook, ws As Worksheet, pt As PivotTable, arr, rng As Range, i As Long
      Application.ScreenUpdating = False: Application.DisplayAlerts = False
      Set wb = Workbooks.Add: arr = Array("Pivot", "Red", "Green")
      ThisWorkbook.Worksheets(arr).Copy Before:=wb.Worksheets(1)
      For i = LBound(arr) To UBound(arr)
        ThisWorkbook.Worksheets(arr(i)).Cells.Copy: wb.Worksheets(arr(i)).Cells(1, 1).PasteSpecial (xlPasteValues)
        For Each pt In ThisWorkbook.Worksheets(arr(i)).PivotTables
          pt.TableRange2.Copy: wb.Worksheets(arr(i)).Range(pt.TableRange2.Address).PasteSpecial (xlPasteFormats)
        Next pt
        wb.Worksheets(arr(i)).Select: ActiveSheet.Cells(1, 1).Select
      Next i
      i = UBound(arr) + 2: While wb.Worksheets.Count >= i: wb.Worksheets(i).Delete: Wend
      wb.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx": wb.Close
      Application.ScreenUpdating = True: Application.DisplayAlerts = True
    End Sub

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

    Re: Copy/Paste Clipboard in VBA

    Hi karedog,

    So I moved

    wb.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx"
    to sit after

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    so it now asks if I want to overwrite the existing file which is great but if I click 'No' then I get an error on

    wb.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx"
    how can I prevent the error to allow the user to rename the file?

    As an alternative I tried replacing

    wb.SaveAs "C:\Client Jobs\Excel Solutions\My New Workbook.xlsx"
    with

    Application.GetSaveAsFilename ("C:\Client Jobs\Excel Solutions\")
    which invokes the Save As dialogue window but if I enter a filename it doesn't save the newly created file using that name, it is still just called Book2 etc., What I'd ideally like is for the Save As dialogue window to appear already populated with the filename thereby allowing the user to append the existing filename, is this possible?

    Many thanks once again
    Last edited by HangMan; 09-04-2015 at 06:41 AM.

  13. #13
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy/Paste Clipboard in VBA

    From the Excel help file :
    Application.GetSaveAsFilename Method
    Displays the standard Save As dialog box and gets a file name from the user without actually saving any files.

    The GetSaveAsFilename only get the string of the filename, it doesn't save, you must call the save method seperately.

    Sub Test4()
      Dim wb As Workbook, pt As PivotTable, arr, i As Long, fileSaveName
      Application.ScreenUpdating = False: Application.DisplayAlerts = False
      Set wb = Workbooks.Add: arr = Array("Pivot", "Red", "Green")
      ThisWorkbook.Worksheets(arr).Copy Before:=wb.Worksheets(1)
      For i = LBound(arr) To UBound(arr)
        ThisWorkbook.Worksheets(arr(i)).Cells.Copy: wb.Worksheets(arr(i)).Cells(1, 1).PasteSpecial (xlPasteValues)
        For Each pt In ThisWorkbook.Worksheets(arr(i)).PivotTables
          pt.TableRange2.Copy: wb.Worksheets(arr(i)).Range(pt.TableRange2.Address).PasteSpecial (xlPasteFormats)
        Next pt
        wb.Worksheets(arr(i)).Select: ActiveSheet.Cells(1, 1).Select
      Next i
      i = UBound(arr) + 2: While wb.Worksheets.Count >= i: wb.Worksheets(i).Delete: Wend
      Application.ScreenUpdating = True: Application.DisplayAlerts = True
      Do
        fileSaveName = Application.GetSaveAsFilename(InitialFileName:="C:\My New Workbook.xlsx", fileFilter:="Excel workbook (*.xlsx), *.xlsx")
        If fileSaveName <> False Then If Len(Dir(fileSaveName)) Then MsgBox "File already exist, please choose another filename" Else Exit Do
      Loop
      wb.SaveAs fileSaveName: wb.Close
    End Sub

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

    Re: Copy/Paste Clipboard in VBA

    Okay, excellent, that is ideal, the only problem now seems to be that if the user hits 'Cancel' instead of 'Save' they can't actually 'Cancel' and make the 'Save As' dialogue window go away? Is there a simple addition to the code to achieve this?

    Also, if they do decide they 'do' want to overwrite the existing file by selecting 'Save', they can't because it invokes the 'This File Already Exists' message.

    This would need to be a warning that they are about to overwrite the existing file, so they should be able to say 'Yes' and overwrite the file or Rename the file if they don't want to overwrite the existing file or hit 'Cancel' to close the Save As dialogue window altogether without doing either...

    Is this possible to achieve?

    Many thanks

  15. #15
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy/Paste Clipboard in VBA

    This is my last post on this thread.

    Sub Test5()
      Dim wb As Workbook, pt As PivotTable, arr, i As Long, fileSaveName
      Application.ScreenUpdating = False: Application.DisplayAlerts = False
      Set wb = Workbooks.Add: arr = Array("Pivot", "Red", "Green")
      ThisWorkbook.Worksheets(arr).Copy Before:=wb.Worksheets(1)
      For i = LBound(arr) To UBound(arr)
        ThisWorkbook.Worksheets(arr(i)).Cells.Copy: wb.Worksheets(arr(i)).Cells(1, 1).PasteSpecial (xlPasteValues)
        For Each pt In ThisWorkbook.Worksheets(arr(i)).PivotTables
          pt.TableRange2.Copy: wb.Worksheets(arr(i)).Range(pt.TableRange2.Address).PasteSpecial (xlPasteFormats)
        Next pt
        wb.Worksheets(arr(i)).Select: ActiveSheet.Cells(1, 1).Select
      Next i
      i = UBound(arr) + 2: While wb.Worksheets.Count >= i: wb.Worksheets(i).Delete: Wend
      Application.ScreenUpdating = True: Application.DisplayAlerts = True
      fileSaveName = Application.GetSaveAsFilename(InitialFileName:="C:\My New Workbook.xlsx", fileFilter:="Excel workbook (*.xlsx), *.xlsx")
      On Error Resume Next: If fileSaveName <> False Then wb.SaveAs fileSaveName
    End Sub

    Regards

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

    Re: Copy/Paste Clipboard in VBA

    Hi karedog,

    That fixed the cancel problem, there is still a slight problem with the 'Save As' but I appreciate you've spent a long time helping me out with this and this was your last post. Your help is hugely appreciated and there is no way I would have been able to get this far without your help, so thank you...

    I'll try and figure out the last little bit on my own.

    Many many thanks

+ 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] Copy and paste text from clipboard
    By plans in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-26-2014, 09:23 PM
  2. [SOLVED] Copy Paste from another sheet w/o clipboard
    By tjw06d in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2014, 10:39 AM
  3. Disable Copy and Paste from Clipboard Buttons
    By Durobell in forum Excel General
    Replies: 6
    Last Post: 08-09-2012, 08:09 AM
  4. Replies: 3
    Last Post: 04-13-2012, 12:26 PM
  5. Copy to Clipboard, concatenate, paste
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-18-2010, 05:15 PM
  6. Copy paste without clipboard
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-11-2010, 03:52 AM
  7. Copy to Clipboard and so that I may paste to any other program
    By tomwashere2 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-22-2006, 10:15 PM

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