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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You cannot change the colour of a forms control button. Would you be happy using a shape instead?
 
Upvote 0
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?
 
Upvote 0
What colour & font do you want?
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
What is the name of the sheet?
 
Upvote 0
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?
 
Upvote 0
Solution

Forum statistics

Threads
1,215,591
Messages
6,125,711
Members
449,252
Latest member
cryss1988

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