Excel 2010 PasteSpecial method of range class failed - vba

I am attempting to understand why a certain bit of code will not work in Excel 2010.
I have a file where cells A1:K200 are part of a table (the Format as Table option form the Home tab), but rows 2-13 are hidden for use with data validation combo boxes. Header row and row banding options are enabled. What I want to do is copy (via the user pressing Ctrl-C) and paste (via user pressing Ctrl-V) data from another workbook and then have a macro function in Worksheet_Change that will transpose the data (based on the value of a checkbox on the worksheet) and paste only the values. Columns A-H will be filled out with other data.
I have disabled events and screen updating, set up a range for cells I14:I200 as StdCost for an intersect function and then have the following code to trap changes to the above range. optTranspose is a checkbox I have placed on the sheet and Target is the variable set up by Excel as part of the Worksheet_Change function.
If Not Application.Intersect(Target, StdCost) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
.Range("I" & Target.Row).PasteSpecial Paste:=xlPasteValues, Transpose:=optTranspose
End If
End If
The above code will work, but based on my testing it will only work with a maximum of 4 values being pasted in OR it will work if I paste starting on row 15 or below. However, if I paste 5+ values I get the error from the subject
PasteSpecial method of range class failed
From my testing, what appears to happen is that before the undo operation kicks in, Excel automatically adds Column1 and Column2 to columns L and M, which then causes Target to take on "Column1" and "Column2" and thus ends up causing the error when I try and run it on the actual values I want to paste. If I convert the table to a normal range, everything appears to operate as expected.
I want to understand why "Column1" and "Column2" are being inserted by Excel, why it's only at 5+ cells from another spreadsheet that it becomes and issue and how I might be able to account for this in my code.

I want to understand why "Column1" and "Column2" are being inserted by Excel,
This happens when you paste data adjacent to an existing ListObject table.
The change event occurs (i.e., you "Paste" the data), the worksheet changes, and then, the Worksheet_Change event fires.
The error seems to happen because Application.Undo is undoing the Paste operation and further affecting the Application.CutCopyMode = False, so on the next line when you attempt to use the PasteSpecial method, there simply is nothing that can be pasted, because the Undo has cleared it.

Pastespecial seems to be very sensitive to the parameters it's fed.
I was experiencing exactly the same error message from code that had worked before migrating to Windows 10. I found this combination of parameters worked in my case:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Can't claim that I found that combo of parameters- it came from yactici's self answer
Kudos to yactici for returning to his initial problem despite having a workaround and directly resolving the problem with pastespecial.

Related

VBA Paste function throwing error after VBA Clear function

I have an advanced filter that is being used to sort a large data set.
The filter is dropping the filtered data into a separate sheet.
I have a VBA Macro that allows me to highlight the portions of the filter that I want to use and paste it into a range adjacent to the filter table.
Currently I am using very simple VBA.
The copy of the active selection and paste into the next open row after a specified Cell. The cell is a row of headers that corresponds to the headers of the table that the copy selection is being made from.
Sub CopyPaste()
Selection.Copy
ActiveSheet.Range("J6").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End Sub
The clear is very simple.
Sub ClearTable()
ActiveSheet.Range("J7:O100").Clear
End Sub
After the clear is run, I receive an error.
Get Run Time error '1004, Application-defined or object defined error.
EDIT: Clarification. .clear and .clearcontents both lead to the errored state If I attempt to paste after I clear the range.
With J7:O100 cleared, select J6 and tap [ctrl]+[down arrow]. Try and go one more row further down. That is where you are trying to paste and if you follow my directions the problem should be perfectly obvious.
Use,
Selection.Copy Destination:=ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
This looks from the bottom up and selects 1 row down not from the top down (which seems to be J1048576 and cannot be moved down a row let alone having room for the paste).

Excel VBA - runtime error 1004 when simplifying recorded code

I've got a recorded macro that I tried to simplify by getting a number of activate and select statements onto a single line, but that results in a runtime error.
This is not a critical problem, but I'm just curious to understand what is going on. This is my initial code snippet (it is preceded by a Copy snippet in the procedure):
ThisWorkbook.Activate
Sheets("MS Act Report").Select
Range("G1").Select
ActiveSheet.Paste
this is my simplified code:
ThisWorkbook.Activate
Sheets("MS Act Report").Range("G1").Select
ActiveSheet.Paste
When running this I get a
runtime error '1004': Select method of Range class failed
You can only select ranges on the active sheet. If "MS Act Report" is not the active sheet, you can't issue a .select command against its cells. To simplify the code, instead of copy-pasting, just make the ranges equal.
Thisworkbook.WorkSheets("MS Act Report").Range("G1:I5").Value= _
ActiveWorkbook.Worksheets("Whatever").Range("a1:c5").Value
Some recommended reading: How to avoid using select

Macro Copy&Paste

I'm trying to create a macro that will copy data from one worksheet and place into another. This I can do with no problem. But, when I want to use the same macro in another row is where I have my problem. Basically what I want to do is copy cell D11 from sheet1 and place that in cell B4 on sheet2, etc (What I'm doing is obviously more complicated than that, but that doesn't matter here).
My problem is when I want to now run this macro and copy cell D12 from sheet1 and paste into B5 on sheet2 the value pasted jumps to B4. I understand that this happens because of where the VBcode is saying to paste the copied value.
My question is how to I just have it paste in whatever row I choose? Maybe based on what row/cell I have selected.
Current code, written by recording the macro
Sheets("sheet1").Select
Range("D11").Select
Selection.Copy
Sheets("sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B4").Select
I'm assuming the last line is where I need to make the change, but I'm not sure what to change.
Thank you! Any and all help is greatly appreciated.
As a general rule, try to avoid Selection Copy-Paste (detailed discussion is provided in: "Application.Calculation = xlCalculationManual" statement causing run-time error 1004 in VBA Copy-Paste procedure). Instead, use direct copy statement, which will solve you issue and significantly improve performance:
Listing 1.
Sub DirectCopySample()
Application.ScreenUpdating = False
Sheets("Sheet1").Range("D11").Copy Destination:=Sheets("Sheet2").Range("B5")
Application.ScreenUpdating = True
End Sub
Sub in Listing 1 performs direct copy from Cell: Sheets("Sheet1").Range("D11") into cell: Sheets("Sheet2").Range("B5").
Also, your initial Copy-Paste Sub could be simplified (it will also make it work, though Listing 1 is preferred)
Listing 2.
Sub CopyPasteSample()
Sheets("sheet1").Range("D11").Copy
Sheets("sheet2").Range("B5").PasteSpecial Paste:=xlPasteValues
End Sub
Hope this will help. Best regards,
You seem to have recorded a Macro and are trying to replay it. Here is a real VBA code (not a Macro recording type):
Sheets("sheet2").Range("B5") = Sheets("sheet1").Range("D11").Value
This is all!
BTW, your predicament comes from the fact that the PasteSpecial method copies into the currently selected cell. You've tried running this Macro several times and the Range("B4").Select line did the trick. If you insist on your approach the insert Range("B5").Select BEFORE the PasteSpecial.

Clear all data after copying it but leaving all functions and formulas

I'd like to either clear the sheet after copying the content of the cells but without deleting the Excel functions, or cut everything from the current region but again, leaving functions in columns H and I intact.
The code is as follows and works well but I don't know how to clear the sheet and leave the functions intact. The functions are in columns H and I
Sub archive_old_data()
'copy Admissions sheet into Archive sheet
Application.ScreenUpdating = False
MsgBox "Please make sure you've reviewed all patients before archiving data."
Worksheets("Admissions").Activate
Range("A1").CurrentRegion.Copy
Worksheets("Archive").Activate
Range("A2").End(xlDown).Offset(1, 0).Select
ActiveCell.PasteSpecial
Worksheets("Admissions").Activate
End Sub
I would appreciate some help.
I don't have to copy currentregion, I need whatever is in cells A2:E2, but the problem is that next time I copy it might be A2:E9, another time A11:E11
If you just want to remove all number-values from a range you can use:
Range("A1:D4").SpecialCells(xlCellTypeConstants, 1).ClearContents
To clear both text and numbers (but not formulas) use SpecialCells(xlCellTypeConstants, 3).
If you are not sure of the specific range then you might apply this to entire columns, or Cells for the entire sheet.
If there is the possibility that there won't be any values to delete then you should include some error-handling, otherwise the error 'No cells were found' appears.

Excel VBA runtime error 1004

I'm trying to write a macro to paste special formulas but keep getting a runtime error 1004 "PasteSpecial method of Range class failed".
This macro comes directly from using the "Record Macro" provided by Excel.
Sub paste_formulas()
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub
Here is the sequence of events:
Manually select a range of cells in CSV file A and copy (CTRL+C).
Switch to the XLS file B and select the cell were I want the copied data pasted.
Press Macros and run personal.xlsm!paste_formulas
That's when I get the error. Why does this work manually when I copy/paste-special formulas, but fail in the macro?
Note: I need this to work in the above sequence regardless of the selected range that I copy (will vary from time to time), and regardless of the location I paste formulas to (will also vary from time to time). In other words, hardcoding a fixed range for copy and/or paste won't work for me.
Thanks in advance for any help understanding why my code isn't working or providing a work-around.
The reason is very simple, When you copy from the CSV and then in your workbook, click on Macros in the Developer Toolbar, Excel clears the clipboard.
Excel has this habit of clearing the clipboard when you click on Developer | Macros. To demonstrate this, copy the cells from the same workbook. You will see the ant like border around the cells. Now in the same workbook, click on Developer | Macros. The Ant like borders will disappear :)
Set a shortcut key for your macro and use that. it will work :)