在Excel中使用VBA更改ActiveX命令按钮的名称

Ehu*_*udz 4 excel vba command

参考

我试图用VBA更改ActiveX命令按钮的名称属性,代码如下:

Set shp = ActiveSheet.Shapes(Selection.Name)

With shp.OLEFormat.Object
    .Object.Caption = "Node" & Str(NumNodes)
    .Name = "Node" & Str(NumNodes)
End With
Run Code Online (Sandbox Code Playgroud)

我可以更改标题名称,但不能使用上面的代码更改name属性.我需要找到一种方法来为name属性连接字符串和int(NumNodes).

UPDATE

这是完整的子例程,它复制命令按钮并将其粘贴到特定的单元格位置.在创建按钮时也会更改属性,例如名称和标题.

Public Sub Node_Button_Duplication()
'
'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


    Set shp = ActiveSheet.Shapes(Selection.Name)

    With shp.OLEFormat.Object
        .Object.Caption = "Node" & Str(NumNodes)
        .Name = "Node" & Str(NumNodes)
    End With

End Sub
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 7

这是你在尝试什么?

Set shp = ActiveSheet.Shapes(Selection.Name)
shp.Name = "Node" & Str(NumNodes)

With shp.OLEFormat.Object
    .Object.Caption = "Node" & Str(NumNodes)
End With
Run Code Online (Sandbox Code Playgroud)

跟进

试过这个就行了......

Public Sub Node_Button_Duication()
    Dim shp As Shape
    Dim NumNodes As Long

    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

    NumNodes = 5

    Set shp = ActiveSheet.Shapes(Selection.Name)
    shp.Name = "Node" & Str(NumNodes)

    With shp.OLEFormat.Object
        .Object.Caption = "Node" & Str(NumNodes)
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

更多关注

试试这个

    Set shp = ActiveSheet.Shapes(Selection.Name)

    With shp.OLEFormat.Object
        .Object.Caption = "Node" & Str(NumNodes)
        .Name = "Node" & NumNodes
    End With
Run Code Online (Sandbox Code Playgroud)

注意,我换Str(NumNodes)NumNodes

ActiveX控件名称不能有空格:)

现在试试.

快照

在此输入图像描述