Search code examples
excelvbacopy-paste

Why is my copy paste pasting the code itself?


This should be simple but i'm clearly doing something wrong. I want to copy cells D88:D90 in sheet "SITE Model" and paste them in sheet "NETWORK Model Results" starting in cell C6. Currently it is pasting out the code as you can see in the attached picture.

Also yes, this should just be one line of code but it wouldn't work so I tried everything haha!

Sheets("SITE Model").Range("D88").Copy
Sheets("NETWORK Model Results").Range("C6").PasteSpecial xlPasteValues
Sheets("SITE Model").Range("D89").Copy
Sheets("NETWORK Model Results").Range("C7").PasteSpecial xlPasteValues
Sheets("SITE Model").Range("D90").Copy
Sheets("NETWORK Model Results").Range("C6").PasteSpecial xlPasteValues

Thank you for all your help this site is amazing!

Pasting out the code itself


Solution

  • It is highly advised not to use copy-paste in VBA Excel macros: the clipboard, where you copy it to, is used over all your Windows platform, so if by any chance you have two applications, doing this, they might overwrite each other's information and mess up everything, therefore I propose following way to re-write your VBA macro:

    Sheets("NETWORK Model Results").Range("C6").Value = Sheets("SITE Model").Range("D88").Value
    Sheets("NETWORK Model Results").Range("C7").Value = Sheets("SITE Model").Range("D89").Value
    Sheets("NETWORK Model Results").Range("C8").Value = Sheets("SITE Model").Range("D90").Value
    

    By the way, I've replaced "C6" by "C8" in the last line, I suppose that "C6" was a typo?