1

During runtime, the user is able to add any number of ActiveX command buttons to Sheet 1. I need to have a reference to these new buttons with VBA, but am not sure how.

I know a logical progression which the button names will exhibit: ex.

(Node#x2)-2=CommandButton#=i

I need to somehow refer to these newly created buttons, I'm thinking is along the lines of this:

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

If anyone knows the correct syntax or an alternate method please advise!

UPDATE

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

Follow-Up

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

This gives me a Run-time error "438: Object doesn't support this property or method. I don't particularly understand

shp.OLEFormat.Object.Object
Ehudz
  • 613
  • 10
  • 22
  • 33
  • How is the user creating the command buttons? Manually or Via Code? – Siddharth Rout May 17 '12 at 15:37
  • When the user clicks an "Add Node" button, an ActiveX button (which exists at start of runtime) will be copied and pasted to an appropriate location. I need to have access to these new buttons so I can change their behavior during runtime. I've also though about creating the buttons repeatedly with VBA, but not sure which is preferable. – Ehudz May 17 '12 at 15:40
  • Can you update the code above that you are using to copy and paste the existing button? Ah I just realized. Is it the same as it was in your previous question? – Siddharth Rout May 17 '12 at 15:41
  • The 438 error is caused by With shp.OLEFormat.Object.Object instead of With shp.OLEFormat.Object – Roy Latham Nov 13 '21 at 01:01

2 Answers2

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

FOLLOWUP

If you know the name of the commandbutton then you can change the properties like this.

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

You can also combine the above two like this

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

And if you need to iterate through all the buttons then use this code.

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
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I already know the name of the command button, but I would like to have access to it later on. For example, I will have 5 buttons, and I would like to access button 3, but don't know the VBA syntax. My guess is something like that given in my question. The button number is an integer variable. Essentially I would like to know how to concatenate the variable to "CommandButton" in something like Sheet1.CommandButton&i.Select – Ehudz May 17 '12 at 16:05
  • What do you mean by Accessing it? Selecting it? Or Something like `Application.Caller`? – Siddharth Rout May 17 '12 at 16:06
  • I would like to change the command button properties, such as caption, macro, deleting, etc. – Ehudz May 17 '12 at 16:08
  • The code is fine. In addition though, I would like to have something like ActiveSheet.Shapes("CommandButton"&Str(i)).Select where I concatenate an integer variable to the string "CommandButton" so I can access any button by the appropriate i variable value. – Ehudz May 17 '12 at 17:21
  • I have included the code which I used but gives me a runtime error – Ehudz May 17 '12 at 17:48
0

Suppose you have a command button (OLE object) with the name 'cmdOriginal' and you want to copy that button and paste it on the same workheet and change the name and caption of the new button into "cmdButtonCopy" and "This is a copy". The newly added button has the highest index in the OLEObjects collection! Place the following code in the code section of the worksheet

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
elmer
  • 1