4

参照

次のコードを使用して、VBA で ActiveX コマンド ボタンの name プロパティを変更しようとしています。

Set shp = ActiveSheet.Shapes(Selection.Name)

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

キャプション名は変更できますが、上記のコードでは name プロパティを変更できません。name プロパティの文字列を int (NumNodes) と連結する方法を見つける必要があります。

アップデート

これは、コマンド ボタンをコピーして特定のセル位置に貼り付ける完全なサブルーチンです。ボタンの作成時に、名前やキャプションなどのプロパティも変更されます。

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
4

1 に答える 1

8

これはあなたがしようとしていることですか?

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 コントロール名にスペースを含めることはできません :)

今すぐやってみて下さい。

スナップショット

ここに画像の説明を入力

于 2012-05-17T19:04:57.077 に答える