Inserting & removing Button using VBA

krissz

Board Regular
Joined
Feb 21, 2010
Messages
95
Hi
I have some code that adds or removes a button, an adaption code I found. I want to be able to set the font & it's colour but am failing. Also the ButtonName has no effect - I would like to set the name.
What have I done wrong ?

Code:
Sub AddButton()
   Dim ctop#, cleft#, cht#, cwdth#
   Dim sht As Worksheet
   Dim Btn As OLEObject
   
   On Error Resume Next
   
   Set sht = ThisWorkbook.Worksheets("Summary")
   With Range("h34")
      ctop = .Top
      cleft = .Left
      cht = .Height
      cwdth = .Width
   End With

   With sht
      Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=cleft, Top:=ctop, Width:=cwdth, Height:=cht)
   End With

   With sht.OLEObjects("CommandButton1").Object
      .BackColor = RGB(199, 21, 133)
      .Name = "ChkAc"
      .Placement = xlMoveAndSize
      .Font.Color = RGB(255, 228, 225)
      .Font.Name = "Trebuchet MS"
      .Font.FontStyle = "Italic"
      .Font.Size = 10
      .Caption = "Check"
     
   End With

End Sub

Sub RemoveButton()

   Worksheets("Summary").OLEObjects("CommandButton1").Delete
   Application.EnableEvents = True

End Sub

Private Sub CommandButton1_Click()

   Dim CustRow As Integer
   
   Worksheets("Overdue Debtors").Select
   CustRow = Range("H33").Value
   ActiveSheet.Cells(CustRow, 1).Select

End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
for font please check the highlighted line
looking @ image below looks like there is no special effect

P4vV-g2PNMlVx6EiaMUY0NwZhi_bZFQtADxAbmIzcWP2mROXu_yqnKM0wliuVLh-E1SaUMGANmU=w1332-h494



Code:
Sub AddButton()
   Dim ctop#, cleft#, cht#, cwdth#
   Dim sht As Worksheet
   Dim Btn As OLEObject
   
   On Error Resume Next
   
   Set sht = ThisWorkbook.Worksheets("Summary")
   With Range("h34")
      ctop = .Top
      cleft = .Left
      cht = .Height
      cwdth = .Width
   End With

   With sht
      Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=cleft, Top:=ctop, Width:=cwdth, Height:=cht)
   End With

   With sht.OLEObjects("CommandButton1").Object
      .BackColor = RGB(199, 21, 133)
      .Name = "ChkAc"
      .Placement = xlMoveAndSize
[U][B]      .ForeColor = RGB(255, 228, 225)[/B][/U]
      .Font.Name = "Trebuchet MS"
      .Font.FontStyle = "Italic"
      .Font.Size = 10
      .Caption = "Check"
     
   End With

End Sub

Sub RemoveButton()

   Worksheets("Summary").OLEObjects("CommandButton1").Delete
   Application.EnableEvents = True

End Sub

Private Sub CommandButton1_Click()

   Dim CustRow As Integer
   
   Worksheets("Overdue Debtors").Select
   CustRow = Range("H33").Value
   ActiveSheet.Cells(CustRow, 1).Select

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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