Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Cut and Paste error

Tags:

excel

vba

I'm trying to cut the contents of cell K7 (100) and paste it into M7 using VBA (see below) but I keep getting an error (see below). Where am I going wrong?:

Sub CutPaste()    
    Worksheets("Sheet2Test").Activate
    Range("K7").Select
    Selection.Cut
    Range("M7").Select
    Selection.Paste   
End Sub

enter image description here

enter image description here

like image 364
Anthony Avatar asked Dec 01 '22 20:12

Anthony


2 Answers

Its better to avoid Select altogether. Use this

Worksheets("Sheet2Test").Range("K7").Cut Worksheets("Sheet2Test").Range("M7")
like image 111
chris neilsen Avatar answered Dec 15 '22 15:12

chris neilsen


Just replace Selection.Paste for ActiveSheet.Paste , so it would be:

Sub CutPaste()    
    Worksheets("Sheet2Test").Activate
    Range("K7").Select
    Selection.Cut
    Range("M7").Select
    ActiveSheet.Paste
End Sub

That would do the paste as you wanted.

like image 38
Nelson Avatar answered Dec 15 '22 15:12

Nelson