VBA: Change the Background of a Created Button - excel

Hello Developers and other, :)
I'm developing a macro to create a lot of button automatically, and I wants to give them a specific background color.
With Worksheets("Sheet 1")
Set r_BtnRange = .Range("O3:O4")
For Each r_Cell In r_BtnRange.Cells
Set b_Btn = .Buttons.Add(r_Cell.Left, r_Cell.Top, _
r_Cell.Width, r_Cell.Height)
With b_Btn
.Caption = "Email End-User"
.OnAction = "Email_EndUser"
.Font.Color = RGB(255, 255, 255)
'.BackColor = 16711680
End With
Next
End With
The .Button.Add return a Button Object, witch has no BackColor Proprety (according to this: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.button?view=excel-pia and the fact I receive an error when tying to uncomment the line)
please see Here
I could use a Shape to do it, but I would know if there is really any method to do it (btw, had to disable it and modify multiple propriety of it.
Thanks a lot for your answers,
Antoine

Related

Hide/unhide multiple shapes Excel VBA

I am very new to excel VBA & macros and I am trying to do something that I think is very simple, but I cannot for the life of me figure it out.
I have a shape ("shape 1") that when clicked should show/unhide two shapes ("shape 2" and "shape 3").
By default "shape 2" and "shape 3" should be hidden and only appear when "shape 1" is selected.
Any help will be much appreciated, remembering I am a complete novice here!
Edit:
I have managed to use the below, essentially a copy paste from here, I dont know what it means but it works for the single button. I cant figure out how to extend the code to include multiple objects being shown/hidden. An example of a second object to be shown/hidden at the same time as "july_2022" is "august_2022".
Public HIDE As Boolean
Sub fy ()
ActiveSheet.Shapes("july_2022").Visible = HIDE
If ActiveSheet.Shapes("july_2022").Visible = False Then
HIDE = True
Else
HIDE = False
End If
End Sub
ActiveSheet.Shapes("july_2022").Visible = HIDE is the part that sets the visibility of
a shape (july_2022). Another identical line but with something other than july_2022 would affect a second shape. The rest of the code (If.. Then.. Else.. End If) could be replaced with HIDE=Not(HIDE).
For example, the following code when run will 'toggle' the visibility of two shapes on the Active Sheet called 'Shape2' and 'Shape3'.
Public HIDE As Boolean
Sub fy()
ActiveSheet.Shapes("Shape2").Visible = HIDE
ActiveSheet.Shapes("Shape3").Visible = HIDE
HIDE = Not (HIDE)
End Sub

Shading selected cell with a certain colour at key press in Excel

I'm a student and I have to learn a lot of stuff/questions, so I came up with an idea and I don't know how to realize it.
So all I am asking is for someone kind to show me how I can program Excel to specifically shade the selected cell with a certain color with a press of a button, as the title suggests.
I want to get in a special mode, where:
I can move around the cells with the arrow keys
by pressing 1 or 2 or 3 or 4, then the selected cell gets shaded with a specific color. (each number for a different color)
The question is, how do I do it? Is something like that possible at all in Excel? I have some background with code, but not even close to a professional. I just did VB Stuff for my GCE A Levels in Computing.
Macros are also possible as I can see but really don't know how to use them.
So here I am, asking this question, hoping someone kind will maybe see it as a challenge and help me realize it. I would invest the time to learn how to do this myself, but with the free time I have now, something like that would take months, just because I'm thorough and I want to learn from the absolute basics.
To enter this special mode you can run a macro to assign new purposes to those keys.
In a module in the VBE Editor, you can add:
Sub EnterSpecialMode()
Call Application.OnKey("1", "color1")
Call Application.OnKey("2", "color2")
Call Application.OnKey("3", "color3")
Call Application.OnKey("4", "color4")
End Sub
Sub ExitSpecialMode()
Call Application.OnKey("1")
Call Application.OnKey("2")
Call Application.OnKey("3")
Call Application.OnKey("4")
End Sub
Sub color1()
Selection.Interior.Color = RGB(255, 0, 0)
End Sub
Sub color2()
Selection.Interior.Color = RGB(0, 255, 0)
End Sub
Sub color3()
Selection.Interior.Color = RGB(0, 0, 255)
End Sub
Sub color4()
Selection.Interior.Color = RGB(255, 255, 0)
End Sub
Self-explanatory, but to enter the special mode you have to run the EnterSpecialMode. And to exit run the ExitSpecialMode sub

Is it possible to address a shape with VBA code?

I am replacing all my ActiveX controls with Excel shapes, because of this well know problem. Therefore I replaced each ActiveX Button with a rectangular shape assigning a macro to each shape:
My question is if I can address those 'shape buttons' with my vba code. Something simple like change the backgroung color of the "Review Start" button should be possible, right?
I'm thinking of something like:
Activesheet.shapes("Review Start").background.colorindex = 1
(This code is obviously not working)
One way is this. Assign a variable to the shape and then you can access its properties and methods easily. I'm not sure there's a way without using RGB.
By declaring the variable as Shape type, Intellisense will show you the properties and methods. Also you can use the Object Browser (F2).
Sub x()
Dim s As Shape
Set s = ActiveSheet.Shapes("Review Start") 'better to specify a sheet name
With s
.Fill.ForeColor.RGB = RGB(255, 255, 255)
.TextFrame.Characters.Font.Color = vbBlack
.TextFrame.Characters.Text = "Fred"
End With
End Sub

Set Button to Unlocked

SO I have a sheet that protect and unprotects in VBA. This works fine for the most part EXCEPT for my form reset button, which does not allow anyone to click the button (coded below).
Is there a way to set a property in the button to unlocked? So that when the macro prior to it creates it and then locks the sheet, you will still be able to click it?
With ActiveSheet.Buttons.Add(545, 42, 72, 18)
.Name = "NEWNCR"
.Caption = "New NCR #"
End With
ActiveSheet.Shapes.Range(Array("NEWNCR")).Select
Selection.OnAction = "populatencrnumber"
Maybe something like
ActiveSheet.Shapes.Range(Array("NEWNCR")).Locked = False
This exact example doesn't work of course. But something like that exists, right? Any help would be appreciated.

Referencing multiple shapes by name

I'm trying to turn multiple shapes (just standard rectangles created by insert -> shapes) yellow when I click on a command button. I selected all the shapes and named them "rect" The name manager acknowledges that the name exists and it includes all the specified shapes. However, when I try to run this
ActiveSheet.Shapes("rect").Fill.ForeColor.RGB = RGB(255, 255, 0)
I get a runtime error "The item with the specified name wasn't found." The weird thing is that this works perfectly fine with just one shape, but when the name refers to multiple shapes then it doesn't work. Any ideas? Thank you.
Select all the shapes as needed and run the code below
Sub Carmello()
Selection.Group.Select
Selection.Name = "rect"
ActiveSheet.Shapes("rect").Fill.ForeColor.RGB = RGB(255, 255, 0)
End Sub

Resources