活动X
您可以使用以下代码将 ActiveX 控件从一张表复制到另一张表。
注意:一个电子表格上不能有两个同名对象。
Sub CopyActiveX()
Application.ScreenUpdating = False
Dim x As OLEObject, y As OLEObject
Set x = Sheets("SRC").OLEObjects("HasCustomName")
Set y = x.Duplicate
Dim xName As String
xName = x.Name
y.Cut
With Sheets("TRGT")
.Paste
.OLEObjects(.OLEObjects.Count).Name = xName
.Activate
End With
Application.ScreenUpdating = True
End Sub
表单控件
要将按钮从一张表复制到另一张表以防止自动更改名称,请使用以下代码。默认情况下,Excel 为复制的按钮(即使在不同的工作表上)赋予一个新名称,因此您必须重命名它以匹配您正在复制的按钮的名称。
使用CopyButton()
sub 来实现它。有4个必需参数
from
- 用于复制按钮的工作表名称
btnName
- 要复制的控件的名称
toWorksheet
- 目标工作表
rng
- 与按钮关联的目标范围
Sub CopyPasteButton()
CopyButton "SRC", "Button 1", "TRGT", "B10"
End Sub
Private Sub CopyButton(from As String, btnName As String, toWorksheet As String, rng As String)
Application.ScreenUpdating = False
Sheets(from).Shapes(btnName).Copy
Sheets(toWorksheet).Activate
Sheets(toWorksheet).range(rng).Select
Sheets(toWorksheet).Paste
Selection.ShapeRange.Name = btnName
Application.ScreenUpdating = True
End Sub