Professional Documents
Culture Documents
com
/vba/cut-copy-paste-with-macro/
In this Article
In this tutorial, you will learn several different methods to Copy & Paste and Cut & Paste using a VBA macro. Read the companion
tutorial on Value Pasting and PasteSpecial for more advanced copying and pasting options.
To use this code: Open the Visual Basic Editor (Alt + F11), Insert a new module (Insert > Module) and copy & paste the desired code
into the module.
Sub Paste_OneCell()
Range("A1").Copy Range("B1")
Range("A1").Cut Range("B1")
End Sub
Copy Selection
If you want to copy the active selection use this:
Sub CopySelection()
Selection.copy range("b1")
Selection.copy
Selection.offset(2,1).paste
Application.CutCopyMode = False
End Sub
Sub Paste_Range()
Range("A1:A3").Copy Range("B1:B3")
Range("A1:A3").Cut Range("B1:B3")
End Sub
Sub PasteOneColumn()
Range("A:A").Copy Range("B:B")
Range("A:A").Cut Range("B:B")
End Sub
Sub Paste_OneRow()
Range("1:1").Copy Range("2:2")
Range("1:1").Cut Range("2:2")
End Sub
Sub Paste_Other_Sheet_or_Book()
Workbooks("book1.xlsm").Worksheets("sheet1").Range("A1").Copy _
Workbooks("book2.xlsm").Worksheets("sheet1").Range("B1") 'Copy
Workbooks("book1.xlsm").Worksheets("sheet1").Range("A1").Cut _
Workbooks("book2.xlsm").Worksheets("sheet1").Range("B1") 'Cut
Application.CutCopyMode = False
End Sub
Value Paste
Normally, when you Copy and Paste you Paste all the properties of a cell: formatting, formulas, etc.. Value Pasting allows you to Copy
and Paste cells’ values and nothing else. The easiest way to Value Paste in VBA is to define the cell’s value directly:
Sub ValuePaste()
Range("B1").value = Range("A1").value
Range("B1:B3").value = Range("A1:A3").value
Worksheets("sheet2").range("A1").value = Worksheets("sheet1").range("A1").value
Workbooks("book2.xlsm").Worksheets("sheet1").range("A1").value = _
Workbooks("book1.xlsm").Worksheets("sheet1").range("A1").value
Application.CutCopyMode = False
End Sub
Paste Special
Paste Special allows you to Copy and Paste specific properties of cells (examples: formats, values, column widths, etc.). It also allows
you to perform special paste operations (examples: skip blanks, transpose). We will look at several examples below, but for an in-depth
read our tutorial on Value Pasting and Paste Special.
Sub PasteSpecial()
Range("A1").Copy
'Paste Formats
Range("B1").PasteSpecial Paste:=xlPasteFormats
Range("B1").PasteSpecial Paste:=xlPasteColumnWidths
'Paste Formulas
Range("B1").PasteSpecial Paste:=xlPasteFormulas
Range("A1").Copy
False, Transpose:=True
Application.CutCopyMode = False
End Sub
Clear Clipboard
After Copying & Pasting you might want to clear the clipboard (we do in some of the code examples above). To clear the Excel
clipboard, we set Application.CutCopyMode to False:
Application.CutCopyMode = False
This will clear Excel’s clipboard. However, it will not clear the Windows Clipboard. To clear the Window’s clipboard follow the
instructions here.