How to stop PasteSpecial popup message

gorgon777

Board Regular
Joined
Mar 12, 2012
Messages
58
Hi I have the below code to iterate and copy/paste data.
Code:
    Application.DisplayAlerts = False
Dim r As Range
Set r = ThisWorkbook.Sheets("POList").Range("A2:A150")
Dim sheetName
sheetName = "Expenses"


Dim XL_File
XL_File = "C:\Kros\"
'Iterate through Unique Number List
For Each cell In r
    Dim val As String
    val = cell.value
    
    'Create Excel file in memory
    Dim XL As Excel.Application
    Dim WBK As Excel.Workbook
    Set XL = CreateObject("Excel.Application")
    Set WBK = XL.Workbooks.Add
    XL.Worksheets(1).Name = "EXP"
    
    ThisWorkbook.Sheets("Data Control").Select
    ThisWorkbook.Sheets("Data Control").Range("A1:U1").Select
    ThisWorkbook.Sheets("Data Control").Range("U1").Activate
    Selection.AutoFilter
    
    ActiveSheet.Range("$A$1:$U$4300").AutoFilter Field:=19, Criteria1:="" & val
    ThisWorkbook.Sheets("Data Control").Columns("A:U").Select
    ThisWorkbook.Sheets("Data Control").Range("U1").Activate
    Selection.Copy
    XL.Sheets(sheetName).Select
    XL.Worksheets(sheetName).Columns("A:U").PasteSpecial Paste:=xlPasteValues


    DoEvents
    WBK.SaveAs (XL_File & CStr(val) & "_Expenses.xlsx")
    WBK.Application.Quit
    XL.Application.Quit
    'XL.Close
    DoEvents
    Application.DisplayAlerts = True
    Application.CutCopyMode = False
Next

This works fine, but I always get a message saying:

"Data on the Clipboard is not the same size and shape as the selected area. Do you want to paste the data anyway?"

The method completes when I click OK, but I am having to do this hundreds of time as there are a lot of files to be created.
Is there a way to stop this message?

With Thanks.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I haven't looked at your code in detail, but try changing this:
XL.Worksheets(sheetName).Columns("A:U").PasteSpecial Paste:=xlPasteValues
to this:
XL.Worksheets(sheetName).Range("A1").PasteSpecial Paste:=xlPasteValues
 
Upvote 0
I haven't looked at your code in detail, but try changing this:
XL.Worksheets(sheetName).Columns("A:U").PasteSpecial Paste:=xlPasteValues
to this:
XL.Worksheets(sheetName).Range("A1").PasteSpecial Paste:=xlPasteValues
Wow, it works! I wonder why its somewhat contradicting the popup message, because surely "A1" is not the same size as "A:U"?

Thanks again, appreciate it:)
 
Upvote 0
Wow, it works! I wonder why its somewhat contradicting the popup message, because surely "A1" is not the same size as "A:U"?

Thanks again, appreciate it:)

You are welcome. When you paste a range to a single cell, Excel assumes that you have chosen the left-most upper cell for the paste and no longer concerns itself with the size & shape of the range to be pasted!
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,819
Members
449,469
Latest member
Kingwi11y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top