VBA PasteAll Question
Results 1 to 6 of 6

Thread: VBA PasteAll Question
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Aug 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA PasteAll Question

    I'm trying to adjust a macro that was using .Select & Pasting pictures, but was throwing a fair amount of Run-time '1004' errors when trying to execute with an RPA tool. I know it's not the cleanest/most efficient, but I'm currently more concerned with the inconsistent results I'm getting. Some companies are correctly populating all the data in every cell, others have #REF ! in all, and some have #REF ! in only some cells. I'm not sure how to correct this.

    Sample below - The first part of the code filters a pivot table from one workbook, pastes it to another after clearing existing data. The second part grabs data from the same workbook and pastes the data to another sheet. I need to maintain the formatting so xlPasteValues is not an option even though it fixes the #REF ! issues.

    Code:
    Windows("CB2.xlsx").Activate
    
    
    ActiveWorkbook.SlicerCaches("Slicer_Property").ClearManualFilter
    For Each Sl_I In ActiveWorkbook.SlicerCaches("Slicer_Property").SlicerItems
    ' Add "Not" after "If" below if you do not want the indicated item to appear
    If Not Sl_I.Value Like "xxxx*" Then Sl_I.Selected = False
    Next
    
    
    'Clear Existing Data
    Workbooks("CB.xlsm").Worksheets("xxxx").Cells.ClearContents
    'Copy and PasteSpecial between workbooks
    Workbooks("CB2.xlsx").Worksheets("Sheet1").Range("A3:E500").Copy
    Workbooks("CB.xlsm").Worksheets("xxxx").Range("A35").PasteSpecial Paste:=xlPasteAll
    
    
    'Paste in the target destination
    Workbooks("CB.xlsm").Worksheets("ClosingBell").Range("A333:H362").Copy
    Workbooks("CB.xlsm").Worksheets("xxxx").Range("A1").PasteSpecial Paste:=xlPasteAll
    'Activate the destination worksheet
    Workbooks("CB.xlsm").Worksheets("xxxx").Activate

  2. #2
    Board Regular MARK858's Avatar
    Join Date
    Nov 2010
    Location
    Southern England
    Posts
    10,995
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA PasteAll Question

    What happens if you paste the values and formats separately? i.e.


    Code:
     Workbooks("CB2.xlsx").Worksheets("Sheet1").Range("A3:E500").Copy
        With Workbooks("CB.xlsm").Worksheets("xxxx").Range("A35")
            .PasteSpecial xlValues
            .PasteSpecial xlFormats
        End With
    Test VBA on a copy of your data (remember you can't normally reverse the action)

    Please follow the forum Rules and Guidelines & please use Code tags around your code i.e. [CODE]your code[/CODE]

    To post a screenshot try one of these links
    MrExcel HTML Maker, RoryA addin (Win & Mac) or Borders-Copy-Paste

  3. #3
    New Member
    Join Date
    Aug 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA PasteAll Question

    I'm getting a '1004' can't paste here as because the Copy area and paste area aren't the same size. Even after changing a couple of the paste areas to test I'm getting - '1004' To do this, all the merged cells need to be the same size.

  4. #4
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    50,383
    Post Thanks / Like
    Mentioned
    54 Post(s)
    Tagged
    11 Thread(s)

    Default Re: VBA PasteAll Question

    Merged cells cause all sorts of problems for VBA and other Excel tasks (like sorting). I strongly encourage you to get rid of them.
    If you are simply merging across rows, you can achieve the same visual effect without all the issues that merged cells cause by using the "Center Across Selection" formatting option.
    See: https://www.atlaspm.com/toms-tutoria...merging-cells/
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  5. #5
    New Member
    Join Date
    Aug 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA PasteAll Question

    I removed all the merged cells and reconfigured all the ranges. However, the last row or two of everything after the first two slicers is only pasting the values, not the format. The headers aren't bold & aligned right, and the percentages are displaying as decimals.

    Code:
    Windows("CB2.xlsx").Activate
    
    
    ActiveWorkbook.SlicerCaches("Slicer_Property").ClearManualFilter
    For Each Sl_I In ActiveWorkbook.SlicerCaches("Slicer_Property").SlicerItems
    ' Add "Not" after "If" below if you do not want the indicated item to appear
    If Not Sl_I.Value Like "xxxx*" Then Sl_I.Selected = False
    Next
    
    
    'Clear Existing Data
    Workbooks("CB.xlsm").Worksheets("xxxx").Cells.ClearContents
    'Copy and PasteSpecial between workbooks
    Workbooks("CB2.xlsx").Worksheets("Sheet1").Range("A3:E500").Copy
    Workbooks("CB.xlsm").Worksheets("xxxx").Range("A35").PasteSpecial Paste:=xlPasteAll
    
    
    'Paste in the target destination
    Workbooks("CB.xlsm").Worksheets("CB").Range("A59:H86").Copy
        With Workbooks("CB.xlsm").Worksheets("xxxx").Range("A1:H27")
                .PasteSpecial xlValues
                .PasteSpecial xlFormats
        End With

  6. #6
    New Member
    Join Date
    Aug 2019
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA PasteAll Question

    Looks like user error. I think I short changed some of the ranges.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •