Random error of range.pastespecial

  • MODERATOR NOTICE: This topic has also been posted on other sites and may already have an answer elsewhere. Please take this into consideration when answering this question

    Dear All,



    Am calling a function from a sub by passing a string (picture path) and a range variable (Cell address).



    The function then inserts the picture and pasts it over range



    Issue: The function gives 1004 error on <range>.pastespecial command but not every time. and pressing F5 in debug mode make it run again till next error.



    Kindly guide if there is some coding error or Logical error



    Application:


    In B Column, there are some image path. say from B2 to B6.


    select B1 to make it active cell and call macro which will loop till B6.


    Pictures will be pasted on Column A for each respective row.



    ----------------------------------------------------------------------

  • Welcome to the Forum. Please read the Forum Rules to understand how the Forum works and why I have added Code Tags to your post


    All VBA code posted in the forum must be wrapped in code tags, which you omitted, including single-line code snippets.Be sure to use them in future posts.


    How to use code tags


    Just highlight all of the code and press the <> in the post menu above button to add the code tags.


    Thanks.

  • Rather than cut and paste, and using the deprecated Pictures.Insert method, you could just use Shapes.Addpicture and position it on the cell when you add it.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Thanks RoyUK and Rory for your time.


    Yes, I got the understanding of using code tags and will use the same for my future posts.


    Yes, I posted this question in multiple forums and am reading all emails for responses received and will mark it done in all those forums.


    I got my query resolved at another board / forum and hence am updating it here as well.



    Below code by Yongle of forum MrExcel.com worked for me


    Link: Using VBA to insert a picture into a cell


  • One More Thing,


    I read the forum rules and also tried to look for some guidance to mark my thread as Resolved / Closed but could not find.


    Any help is appreciated.


    I looked in this thread but could not find a way on how it was done except that an Administrator confirmed it.


    Changing the Status of a thread

  • BTW the real solution to my problem was to add

    Code
    1. Application.CutCopyMode = False

    making it


    Code
    1. Application.CutCopyMode = False
    2. picobject.Cut
    3. TargetCell.PasteSpecial
  • That's a solution. IMO you would still be better off using Shapes.Addpicture (otherwise you have no control over whether the picture is embedded or linked) and simply putting it in the right place to start with.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why