Generating a new command button by clicking a command button and adding code to the new button.

ExcelQs

New Member
Joined
Jul 12, 2012
Messages
22
Hi. I've made a code that will create, position and name a new command button when a command button that already exisits is pushed. The next step is to insert code into the new button. I think the best way to do this is to put the new button code in a module, which can then be copied and pasted into the new button. I'm having trouble figuring that part out. Here is part of my code so far:

Code:
 '---Insert Button--------------------------------------------------
            Dim oleButton As OLEObject
            Dim bolSkip As Boolean
            Dim wsCvrSheet As Worksheet
            
            Set wsCvrSheet = Worksheets("Cover Sheet")
            
            'Turn screen updating off
            Application.ScreenUpdating = False
    
            'Check that button is not already entered
            For Each oleButton In wsCvrSheet.OLEObjects
                If oleButton.Name = "Unit 1" Then bolSkip = True
            Next
    
            'If Button does not exist
            If Not bolSkip Then
                'Pick cell where top right hand corner of button will be
                Dim rng As Range
                Dim ctop#, cleft#, cht#, cwdth#
                Set rng = Cells(1, 5)
                With rng
                    ctop = .Top
                    cleft = .Left
                    cht = .Height
                    cwdth = .Width
                End With
                
                With wsCvrSheet
                    'Add the button to the sheet with specified dimensions
                    Set oleButton = wsCvrSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1" _
                    , Link:=False, DisplayAsIcon:=False, Left:=cleft, Top:=ctop, Width:=3 * cwdth, _
                    Height:=2 * cht)
                End With
                
                '---Add code to button----
                '    Dim i As Long
                 '   Dim cmdArray() As New Class1
                    
                  '  i = 1
                    
                   ' ReDim Preserve cmdArray(1 To i)
                    'Set cmdArray(i).CmdEvents = oleButton
                '-----------------------------
                    
                'Rename the Button, so that the code added later refers to it
                oleButton.Name = "Unit 1"
                oleButton.Object.Caption = "Unit 1"
                
                Set rng = Nothing
                
                    
            Else
                MsgBox "Button already exists"
            End If
     
            'Turn screen updating on
            Application.ScreenUpdating = True
    
            Set wsCvrSheet = Nothing
            Set oleButton = Nothing
        'End Insert Button-------------------------------------------------------------
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Here is what I would do...

Code:
Sub makeButton(rng)
    On Local Error Resume Next
    Dim sname
    sname = Sheets("Cover Sheet").Buttons("Unit 1").Name
    If Err.Number <> 0 Then
        With rng
            Top = .Top
            cleft = .Left
            cht = .Height
            cwdth = .Width
        End With
        With Sheets("Cover Sheet").Buttons.Add( _
            Left:=cleft, Top:=ctop, Width:=3 * cwdth, Height:=2 * cht)
                    
            .OnAction = "Unit1_Click"
            .Characters.Text = "Unit 1"
            .Name = "Unit 1"
        End With
    End If
End Sub

To write code that is executed on click (OnAction):

Code:
Sub Unit1_Click()
    MsgBox Application.Caller & " was clicked"
End Sub

To create a button:
Code:
Sub TEST()
    makeButton Selection
End Sub

Let me know if this helps...
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,826
Members
449,190
Latest member
rscraig11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top