在Excel中使用VBA运行时添加的命令按钮的引用

时间:2012-05-17 15:22:29

标签: excel vba button runtime

在运行时,用户可以向Sheet 1添加任意数量的ActiveX命令按钮。我需要使用VBA引用这些新按钮,但不确定如何。

我知道按钮名称将显示的逻辑进展:ex。

(节点#X2)-2 =命令按钮#= I

我需要以某种方式引用这些新创建的按钮,我的想法是这样的:

Sheet1.Controls("CommandButton" & i).Select

如果有人知道正确的语法或替代方法,请告知!

更新

Public Sub Node_Button_Duplication()
'
'Comments: Copies and pastes Node 1's button to the appropriate column

' Copy Node 1 button and paste in appropriate location
    ActiveSheet.Shapes("CommandButton1").Select
    Selection.Copy
    Cells(5, 10 + 7 * (NumNodes - 1) - 1).Select
    ActiveSheet.Paste
    Selection.ShapeRange.IncrementLeft 47.25
    Selection.ShapeRange.IncrementTop -13.5


End Sub

后续

Public Sub Node_Button_Duication()
'
'Comments: Copies and pastes Node 1's button to the appropriate column

Dim shp As Shape

' Copy Node 1 button and paste in appropriate location
    ActiveSheet.Shapes("CommandButton1").Select
    Selection.Copy
    Cells(5, 10 + 7 * (NumNodes - 1) - 1).Select
    ActiveSheet.Paste
    Selection.ShapeRange.IncrementLeft 47.25
    Selection.ShapeRange.IncrementTop -13.5

    Debug.Print Selection.Name

    Set shp = ActiveSheet.Shapes(Selection.Name)

    With shp.OLEFormat.Object.Object
        .Caption = "Test"
        .Left = 15
        .Top = 15
    End With

End Sub

这给了我一个运行时错误“438:对象不支持这个属性或方法。我不是特别理解

shp.OLEFormat.Object.Object

2 个答案:

答案 0 :(得分:4)

Public Sub Node_Button_Duplication()
    ActiveSheet.Shapes("CommandButton1").Select
    Selection.Copy
    Cells(5, 10 + 7 * (NumNodes - 1) - 1).Select
    ActiveSheet.Paste
    Selection.ShapeRange.IncrementLeft 47.25
    Selection.ShapeRange.IncrementTop -13.5

    '~~> This will give you the name
    Debug.Print Selection.Name
End Sub

<强>后续

如果您知道命令按钮的名称,那么您可以更改这样的属性。

Option Explicit

Sub Sample()
    Dim shp As Shape

    '~~> Since you already have the name replace "CommandButton1" by
    '~~> the name that you have
    Set shp = ActiveSheet.Shapes("CommandButton1")

    With shp.OLEFormat.Object
        .Object.Caption = "Test"
        .Left = 15
        .Top = 15
    End With
End Sub

您也可以将上述两个结合起来

Public Sub Node_Button_Duplication()
    Dim shp As Shape

    ActiveSheet.Shapes("CommandButton1").Select
    Selection.Copy
    Cells(5, 10 + 7 * (NumNodes - 1) - 1).Select
    ActiveSheet.Paste
    Selection.ShapeRange.IncrementLeft 47.25
    Selection.ShapeRange.IncrementTop -13.5

    '~~> This will give you the name
    Debug.Print Selection.Name

    Set shp = ActiveSheet.Shapes(Selection.Name)

    With shp.OLEFormat.Object
        .Object.Caption = "Test"
        .Left = 15
        .Top = 15
    End With

End Sub

如果您需要遍历所有按钮,请使用此代码。

Sub CommanButtons()
    Dim wks As Worksheet
    Dim OLEObj As OLEObject

    '~~> set it as per the relevant sheet
    Set wks = Worksheets("sheet1")

    For Each OLEObj In wks.OLEObjects
        If TypeOf OLEObj.Object Is MSForms.CommandButton Then
            Debug.Print OLEObj.Object.Caption
        End If
    Next OLEObj
End Sub

答案 1 :(得分:0)

假设您有一个名为“ cmdOriginal”的命令按钮(OLE对象),并且您想要复制该按钮并将其粘贴在同一工作表上,并将新按钮的名称和标题更改为“ cmdButtonCopy”和“ This”是副本”。 新添加的按钮在OLEObjects集合中具有最高的索引! 将以下代码放在工作表的代码部分中

Sub x1()
    Me.OLEObjects("cmdOriginal").Copy
    Me.Paste
    With Me.OLEObjects(Me.OLEObjects.Count)
        .Name = "cmdButtonCopy"
        .Caption = "This is a copy"
    End With
End Sub