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!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,444
Office Version
  1. 365
Platform
  1. Windows
You cannot change the colour of a forms control button. Would you be happy using a shape instead?
 

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
You cannot change the colour of a forms control button. Would you be happy using a shape instead?
Yes that would be great as long as I can incorporate it into my VBA code. Do you have any ideas to implement that?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,444
Office Version
  1. 365
Platform
  1. Windows
What colour & font do you want?
 

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

What colour & font do you want?
I wanted the button to be dark blue and the font color to be white. Thanks!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,444
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
Sub CreateButton()
Dim btn As Shape

Application.ScreenUpdating = False
On Error Resume Next
ActiveSheet.Shapes("Button").Delete
On Error GoTo 0

With ActiveSheet.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
 

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Ok, how about
VBA Code:
Sub CreateButton()
Dim btn As Shape

Application.ScreenUpdating = False
On Error Resume Next
ActiveSheet.Shapes("Button").Delete
On Error GoTo 0

With ActiveSheet.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
Thanks! The button color and font have changed but it no longer seems to be running the macros correctly like my previous code did. Also if I only want to apply this code on one worksheet in my workbook and I know the name of that worksheet how would I modify the code?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,444
Office Version
  1. 365
Platform
  1. Windows
What is the name of the sheet?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,444
Office Version
  1. 365
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?
 
Solution

Forum statistics

Threads
1,141,410
Messages
5,706,292
Members
421,439
Latest member
JordsdoExcel

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