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

参考

我想用下面的代码用VBA来更改ActiveX命令button的名称属性:

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

我可以更改标题名称,但名称属性不能用上面的代码进行更改。 我需要find一种方法来连接string与一个int(NumNodes)的名称属性。

UPDATE

这是复制命令button并将其粘贴到特定单元格位置的完整子例程。 诸如名称和标题的属性也在创buildbutton时被改变。

 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 

这是你正在尝试?

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

跟进

刚试过这个,它工作…

 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 

更多后续

尝试这个

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

请注意,我将Str(NumNodes)更改为NumNodes

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

现在试试。

快照

在这里输入图像说明