copy and paste selection in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,644
Office Version
  1. 365
Platform
  1. Windows
Hi

I created 2 command buttons one to copy whatever cells are selected using the mouse and then another button to paste them in another selected area/cells. I am getting error message (not accepting Selection.paste). Please see code below. Also can I ask you what "Selection" is? is it an object or what. I could not find it listed under objects list.

Thanks

Private Sub CommandButton1_Click()
Selection.Copy
End Sub
Private Sub CommandButton2_Click()
Selection.Paste
End Sub
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
1,690
Hi

i think you may need to specify your paste

like, pastespecial

if you use the macro recorder you can pick your desired paste type.

Dave
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,253
Office Version
  1. 2013
Platform
  1. Windows
Try this:

Code:
Private Sub CommandButton1_Click()
 Selection.Copy
 End Sub
 Private Sub CommandButton2_Click()
 Selection.PasteSpecial
 End Sub
 

ping_pong

Board Regular
Joined
Dec 27, 2015
Messages
54
Selection is a range object, yes.

If you post your full code or a sample worksheet if will be much easier to help you. Anyways, as posted, you can use the PasteSpecial method. You can also use ActiveSheet.Paste.
 

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,644
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Thank you all for your help but Selection. Pastespecial did not work? "past especial method of the range class failed" msg

Code:
Sub cutselection()
    selection.Cut
End Sub
Code:
Sub pasteselection()
    selection.PasteSpecial
End Sub
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
771
Rich (BB code):
Sub cutselection()
    selection.Cut
End Sub

"copy" and "cut" are not the same. Go back to "copy"
Code:
Selection.Copy

and it should work.

Code:
[COLOR=#333333]
Sub copyselection()
    Selection.Copy
End Sub[/COLOR]
[COLOR=#333333]
Sub pasteselection()
[/COLOR]    Selection.PasteSpecial Paste:=xlPasteAll[COLOR=#333333]
End Sub[/COLOR]
 

Watch MrExcel Video

Forum statistics

Threads
1,122,841
Messages
5,598,390
Members
414,234
Latest member
grlevesq

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top