I am using the code below to copy a command button from one sheet and paste it into another:
Sheets("SRC").HasACustomName.Copy
Sheets("TRGT").Range("O1").PasteSpecial
When I paste it, it get's renamed from HasACustomName
to CommandButton1
.
Can I either copy/paste it in a way that retains the name or change the name after pasting?
ActiveX
You can copy an ActiveX Control from one sheet to another with the below code.
Note: you cannot have two objects of the same name on one spreadsheet.
Form Control
To copy a button from one sheet to another preventing the automatic name change use the below code. Excel by default gives a new name to a copied button (even on a different sheet) so you have to rename it to match the name of the button youre copying.
Use
CopyButton()
sub to achieve it. There are 4 required parametersfrom
- sheet name to copy the button frombtnName
- the name of the control you want to copytoWorksheet
- target worksheetrng
- target range to associate with the button