2012-05-17 62 views
4

Reference更改姓名

我想要的的ActiveX命令按鈕的名稱屬性與VBA用下面的代碼更改:

Set shp = ActiveSheet.Shapes(Selection.Name) 

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

我能夠改變標題名稱,但名稱屬性不能使用上面的代碼進行更改。我需要找到一種方法來連接字符串與一個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 

回答

7

這是你正在嘗試?

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控件名稱不能有空格:)

現在就來試試。

快照

enter image description here

+0

是。結果是命令按鈕名稱保持爲「CommandButton4」。 – Ehudz

+0

我剛試過。它會更改新創建的命令按鈕的名稱 –

+0

您確定它會更改** NAME **和**標題**,而不僅僅是**標題**嗎?在VB編輯器中,名稱字段在創建按鈕後仍然說CommandButton4 – Ehudz