VBA PasteAll Question

nrie5

New Member
Joined
Aug 22, 2019
Messages
4
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
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
12,860
Office Version
365, 2010
Platform
Windows, Mobile
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
 

nrie5

New Member
Joined
Aug 22, 2019
Messages
4
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.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,644
Office Version
365
Platform
Windows

nrie5

New Member
Joined
Aug 22, 2019
Messages
4
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
 

nrie5

New Member
Joined
Aug 22, 2019
Messages
4
Looks like user error. I think I short changed some of the ranges.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,099
Messages
5,484,660
Members
407,460
Latest member
Fakxi

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top