Transfer CommandButtons to another workbook using VBA

marimar02

Board Regular
Joined
May 21, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to Copy 4 ActiveX CommandButtons from one Workbook, each named "CommandButton1", 2, 3 and 4"and Paste onto another. I have a code that successfully copies it over but if the Target Workbook already has a CommandButton1-4, the copied Button will continue in sequence and name itself "CommandButton5".

How can I ensure that all buttons are deleted from the Target Workbook and the new button is named the same as in the Source Workbook.

Here are the codes I use to delete all buttons in Source Workbook:

Code:
On Error Resume Next    
ActiveSheet.DrawingObjects.Visible = True
ActiveSheet.DrawingObjects.Delete
On Error GoTo 0

Here is the code to Copy buttons from Source to Target Worksheet:

Code:
Source.Sheets("Journal Entry").Activate    
ActiveSheet.Shapes.Range(Array("CommandButton1")).Select
Selection.Copy
Target.Sheets("Journal Entry").Activate
ActiveSheet.Paste
ActiveSheet.Shapes.Range(Array("CommandButton1")).Select 'this is where the code errors out since the Paste is renamed to CommandButton5
Set Button = ActiveSheet.Shapes("CommandButton1")
Button.Left = 622.5
Button.Top = 48

Thanks much...
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Thanks but I need to know how to copy buttons in sequence. Buttons are still named randomly. I.e. I'll copy/paste CommandButton1 as CommandButton1, 2 as 2, then 3 pastes as 5. Why doesn't it paste in sequence?
 
Upvote 0
Why don't you use forms buttons? Then it doesn't matter what they're called, nor do you need to copy them
 
Upvote 0
Good point. I can rebuild but I remain puzzled why Excel keeps assigning different numbers to command buttons even if i specify the correct name.
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,288
Members
448,563
Latest member
MushtaqAli

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