Excel VBA runtime error 1004 - vba

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 :)

Related

PasteSpecial in secondary Excel window changes worksheet in primary window

I copy and paste formulas frequently, so I wrote a short macro to give me a keyboard shortcut:
Sub PasteFormula()
' Keyboard Shortcut: Ctrl+Shift+F
Dim TargetRange As Range
If Application.CutCopyMode Then
Set TargetRange = Application.ActiveWindow.Selection
TargetRange.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
End Sub
It works exactly as desired, with one undesired pecularity. Here's how to create a MWE:
Open a new workbook Book1.xlsb. Copy the VBA above into a module, and link up the keyboard shortcut Ctrl+Shift+F.
Use View -> Window -> New Window to create a second window, so that the windows' titles are "Book1:1" and "Book1:2".
In the window "Book1:2" create a second sheet "Sheet2" and open that sheet. Type something in one cell, copy it, go to another cell on the same window, and press Ctrl+Shift+F.
The macro will work--the formula from the first cell is copied, as a formula, into the second cell. However, the first window "Book1:1" changes to display Sheet2. How can I use VBA to run PasteSpecial in a secondary window without changing sheets in the primary window?
Clarification
I am working in just one workbook, "Book1.xlsb", open in two windows. That workbook has two sheets; I have Sheet1 open in the first window and Sheet2 open in the second window.
When I run a PasteSpecial in Sheet2 in the second window (Book1:2), the first window (Book1:1) switches to Sheet2 as well.
For contrast, if I run a PasteSpecial in the first window (Book1:1), the second window does not switch sheets.
I think this behavior is strange. So my questions are
Why does Excel do this, and/or
How can I make Excel not do this?

Cant Use Copy and Paste when I have and Excel VBA Macro Running

Whenever I run a certain report macro in Excel I cannot use Copy and Paste elsewhere. For instance say I start up the report(depending on how many lines can take 30+ minutes) and then go work elsewhere while its running in the background it doesn't allow me to copy and paste. The only thing it does paste is whatever the Macro is currently working on and If I try to copy something it gets inserted into the next cell on the report messing that report up.
Looking for any advice or if anyone else has heard of this.
Thanks.
Do you even need copy/paste? Perhaps under the covers VBA isn't really copy/pasting, per-se, but if you only need the values, why not:
Range("B1").Value = Range("A1").Value
Bear in mind you can truly do this for ranges, not just cells
Range("B1:B100").Value = Range("A1:A100").Value
This won't copy formats and such, but you can add those as well:
Range("A1").NumberFormat = Range("B1").NumberFormat
This may not be as tidy, code-wise as what you seek with a copy/paste, but I'd encourage you to benchmark it and see which is more efficient. Again, perhaps it's all the same under the hood; I really don't know.
By using:
Range("A1").Select
Selection.Copy
Range("B1").Select
Selection.Paste
I was using my Active Clipboard ergo why I couldn't Copy and Paste by altering it to:
Range("A1?).Copy Destination:=Range("B1?)
It will do the same thing only not use your active clipboard and be way more efficient.

Excel 2010 PasteSpecial method of range class failed

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.

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.

Copy ppt into Excel with formatting

I am trying to copy from powerpoint to excel and trying to keep the power point formating.
For x = 1 To pptApp.ActivePresentation.Slides.Count
pptApp.ActivePresentation.Slides.Range(Array(x)).Select
pptApp.ActivePresentation.Slides.Range(Array(x)).Copy
pptApp.Windows(2).Activate
pptApp.ActiveWindow.View.Paste
pptApp.Windows(2).Activate
Next x
I'm currently using this to do the copy and it works fine, however it doesn't copy the formatting.
Any Ideas how to achieve this?
Sp
On top of PaulStock's valid point, when you paste from one office application to another the buffer can be interpreted in many different ways by the importing program. In your case you want to make sure that Excel imports the PPT slide not as HTML, text, BMP or any other, but as a slide object. In the interactive mode you would use the PasteSpecial command, and a VBA aequivalent in Excel would be
[SheetObject].PasteSpecial Format:="Microsoft PowerPoint Slide Object", _
Link :=False, _
DisplayAsIcon:=False
quick & dirty testing: put this code in a sheet-onDoubleClick trigger, replacing [SheetObject] by "Me", move to PPT, copy a slide from the slide strip (!!!), go back to Excel and doubleclick a cell - and voi lá!
(here Office 2003 SP3)