Adding Color and Changing Font of a Button made using VBA

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I currently have code to create a button that goes into A1 and I would like to change the color and font of the button. How can I add to my existing code to do so?

My Current Code:

VBA Code:
Sub CreateButton()
Dim btn As Button
Dim t As Range

Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete

Set t = ActiveSheet.Range(Cells(1, 1), Cells(1, 1))
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)

With btn
.OnAction = "Button" 'Runs Macro Button 
.Caption = "Run Macros"
.Name = "Button"

Application.ScreenUpdating = True

End Sub

Thanks for any help in advance!
 

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
In that case you can use
VBA Code:
Sub CreateButton()
Dim btn As Shape

Application.ScreenUpdating = False

On Error Resume Next
Sheets("Data_1").Shapes("Button").Delete
On Error GoTo 0

With Sheets("Data_1").Range("A1")
   Set btn = .Worksheet.Shapes.AddShape(1, .Left, .Top, .Width, .Height)
End With
With btn
   .OnAction = "Button" 'Runs Macro Button
   .TextFrame.Characters.Caption = "Run Macros"
   .Fill.ForeColor.RGB = rgbDarkBlue
   .TextFrame.Characters.Font.Color = vbWhite
   .Name = "Button"
End With
Application.ScreenUpdating = True

End Sub
Is the macro you want to run simply called Button?
I got it to work! I realized I had not saved it as a macro enabled workbook and since I was using macros from my personal workbook it was having some issues.

Thank you so much for the help!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,243
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,141,051
Messages
5,703,967
Members
421,321
Latest member
blusky4

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
Top