VBA Excel Runtime Error VBA 1004

I am trying to write a macro to insert special formulas, but keep getting a run-time error 1004 "PasteSpecial class method Range failed".

This macro comes directly from the "Recording Macros" 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 the cell range in the CSV A file and copy (CTRL + C).
  • Switch to XLS file B and select a cell to copy the copied data.
  • Click Macros and run personal.xlsm! paste_formulas

This is when I get the error. Why does this work manually when I copy / paste special formulas but don't execute a macro?

Note. I need this to work in the above sequence, regardless of the range that I copy (it will change from time to time), and regardless of where I paste the formulas (it will also change from time to time). In other words, hardcoding a fixed range for copy and / or paste will not work for me.

Thanks in advance for understanding why my code doesn't work or doesn't work.

+3
source share
1 answer

The reason is very simple. When you copy CSV, and then in your Macrosworkbook , click on the developer toolbar, Excel will clear the clipboard.

Excel , Developer | Macros. , . ant . " | . ant :)

enter image description here

. :)

enter image description here

+3

All Articles