copy and paste selection in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
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
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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
 
Upvote 0
Try this:

Code:
Private Sub CommandButton1_Click()
 Selection.Copy
 End Sub
 Private Sub CommandButton2_Click()
 Selection.PasteSpecial
 End Sub
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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]
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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
Back
Top