Programmatically Adding Buttons to a Worksheet

Festus Hagen

New Member
Joined
Aug 1, 2011
Messages
40
Hi All,

Adding buttons, After lots of diggin and reading I have finally gotten it to work ONE button at a time ... It crashes on the second iteration. (when it attempts to Add the second button)

Here is where I am at, If I add an "Application.ActiveWorkbook.Save" at the end of the loop before it iterates the second time, the first button is created and works fine (after re-opening the wb)

If I remove the Code Adding 'With' block it works fine, whats the point without the code for them to act upon ...

Setting ooButton.Name is pointless, it appears it's not the correct property that matches the procedure name ... No big deal, I really don't care about the button names or procedure names as long as they work!
My choice would be:
Code:
  ooButton.Name = Format(rRange.Offset(1), "mmmmdd")
  ooButton.OnAction = Format(rRange.Offset(1), "mmmmdd")
Never could get them to hook up, tried many different methods, Different Quotes, etc ...

For cleanup/testing ...
Code:
Sub DelButtons()
Dim oObject As OLEObject
  Sheet2.Activate
  For Each oObject In Sheet2.OLEObjects
    oObject.Delete
  Next
End Sub

Code:
Sub AddButtons()
Dim rRange As Range
Dim ooButton As OLEObject
Dim lLines As Long
Dim sString As String
    
  Sheet2.Activate

  Set rRange = Sheet2.Range("B1")
  While rRange.Value <> ""
    Set ooButton = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False, Left:=rRange.Left, _
      Top:=rRange.Top, Width:=rRange.Width, Height:=rRange.Height)
    ooButton.Placement = xlMoveAndSize
    ooButton.PrintObject = False
    ooButton.Object.Caption = rRange.Value
    ooButton.Object.TakeFocus******* = False
    sString = vbNewLine & _
      "Private Sub " & ooButton.Name & "_Click()" & vbCrLf & _
      "  ActiveWorkbook.Names(" & Chr(34) & "PicksPasteZone" & Chr(34) & ").RefersTo = " & Chr(34) & "=" & rRange.Offset(2).Address & vbCrLf & _
      "End Sub" & vbNewLine
    With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
      .InsertLines 2, sString
    End With
    
    Set ooButton = Nothing
    Set rRange = rRange.Offset(, 1) ' Step by Column
  Wend
End Sub

Thanks y'all!

-Enjoy
fh <font color="#FF0000">:</font> )_~
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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