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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
1,998
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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
18,708
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,805
Office Version
  1. 2016
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
 

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
1,998
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
change

Selection.pastespecial

To

Paste

Dave
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
1,411
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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]
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,026
Messages
5,835,024
Members
430,332
Latest member
Charly_Moon

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